Skip to content

Commit

Permalink
feat(P.infer): Remove inference point
Browse files Browse the repository at this point in the history
  • Loading branch information
gvergnaud committed Dec 14, 2024
1 parent b5aa458 commit b6d791a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
"typescript": "^5.7.2"
}
}
10 changes: 3 additions & 7 deletions src/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ export type unstable_Matcher<
* const userPattern = { name: P.string }
* type User = P.infer<typeof userPattern>
*/
export type infer<pattern extends Pattern<any>> = InvertPattern<
pattern,
unknown
>;
export type infer<pattern> = InvertPattern<NoInfer<pattern>, unknown>;

/**
* `P.narrow<Input, Pattern>` will narrow the input type to only keep
Expand All @@ -110,9 +107,8 @@ export type infer<pattern extends Pattern<any>> = InvertPattern<
* type Narrowed = P.narrow<Input, typeof Pattern>
* // ^? ['a', 'a' | 'b']
*/
export type narrow<input, pattern extends Pattern<any>> = ExtractPreciseValue<
input,
InvertPattern<pattern, input>
export type narrow<input, pattern extends Pattern<any>> = NoInfer<
ExtractPreciseValue<input, InvertPattern<pattern, input>>
>;

function chainable<pattern extends Matcher<any, any, any, any, any>>(
Expand Down
2 changes: 1 addition & 1 deletion src/types/FindSelected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ export type FindSelected<i, p> =
// This happens if the provided pattern didn't extend Pattern<i>,
// Because the type checker falls back on the general `Pattern<i>` type
// in this case.
Equal<p, Pattern<i>> extends true ? i : Selections<i, p>;
NoInfer<Equal<p, Pattern<i>> extends true ? i : Selections<i, p>>;
17 changes: 17 additions & 0 deletions tests/infer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ describe('P.infer', () => {
type test2 = Expect<Equal<QuizState, expected2>>;
});
});

it("P.infer shouldn't count as an inference point of the pattern", () => {
const getValueOfType = <T extends P.Pattern<unknown>>(
obj: unknown,
path: string,
pattern: T,
defaultValue: P.infer<T>
): P.infer<T> => defaultValue;

getValueOfType(
null,
'a.b.c',
{ x: P.string },
// @ts-expect-error 👇 error should be here
'oops'
);
});
});
4 changes: 3 additions & 1 deletion tests/not.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ describe('not', () => {
.exhaustive()
).toBe('hello');

const untypedNullable = P.when((x) => x === null || x === undefined);
const untypedNullable = P.when(
(x): boolean => x === null || x === undefined
);

expect(
match<{ str: string }>({ str: 'hello' })
Expand Down

0 comments on commit b6d791a

Please sign in to comment.