Skip to content

Commit

Permalink
update type
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan7237d committed Jul 3, 2024
1 parent dea636b commit 60f608e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

---

Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Pipe = {
<T, A>(source: T, a: (value: T) => A): A;
<T, A = T>(source: T, a: (value: T) => A): A;
<T, A, B>(source: T, a: (value: T) => A, b: (value: A) => B): B;
<T, A, B, C>(
source: T,
Expand Down

0 comments on commit 60f608e

Please sign in to comment.