From 60f608e28cfe84485eb4f2e550bfcf3509ad8f74 Mon Sep 17 00:00:00 2001 From: Ivan Novikov Date: Wed, 3 Jul 2024 09:07:39 -0400 Subject: [PATCH] update type --- .github/README.md | 2 +- CHANGELOG.md | 4 +++- package.json | 2 +- src/index.test.ts | 7 +++++-- src/index.ts | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/README.md b/.github/README.md index 953186b..ff851ab 100644 --- a/.github/README.md +++ b/.github/README.md @@ -28,7 +28,7 @@ import { pipe } from "pipe-function"; Takes between 2 and 20 arguments. `pipe(x, a, b)` is equivalent to `b(a(x))`, in other words, this function pipes a value through a number of functions in the order that they appear. [This article](https://dev.to/ivan7237d/i-ve-used-the-pipe-function-2-560-times-and-i-can-tell-you-it-s-good-4aal) talks about why this function is useful. -`pipe(x)` will run and return `x`, but will produce a type error. +When you have a single argument, like `const y = pipe(x)`, `pipe` is redundant, so you will get a type error, but the code will run and return `x`. Despite the type error, the type of `y` will be inferred correctly as type of `x`. --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 6361e43..c2d4f66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ # Changelog -All notable changes to this project will be documented in this file. +## [1.0.1] + +Type signature changed in such a way that when you write `const y = pipe(x)`, you still get a type error, but the type of `y` is inferred as type of `x` instead of `unknown`. diff --git a/package.json b/package.json index d6ec15d..e52e91a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pipe-function", - "version": "1.0.0", + "version": "1.0.1", "description": "A function to pipe a value through a number of transforms", "repository": "https://github.com/ivan7237d/pipe-function.git", "license": "MIT", diff --git a/src/index.test.ts b/src/index.test.ts index 3e34c61..cab3808 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -6,8 +6,11 @@ const addSuffix = `${base}-${suffix}`; test("", () => { - // @ts-expect-error - expect(pipe("base")).toMatchInlineSnapshot(`"base"`); + // $ExpectType "base" + const result = + // @ts-expect-error + pipe("base" as const); + expect(result).toMatchInlineSnapshot(`"base"`); expect( // $ExpectType "base-a" diff --git a/src/index.ts b/src/index.ts index 68c77be..db358c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ type Pipe = { - (source: T, a: (value: T) => A): A; + (source: T, a: (value: T) => A): A; (source: T, a: (value: T) => A, b: (value: A) => B): B; ( source: T,