Suggestion: check narrowed type in user-defined type guards
See original GitHub issueSearch Terms
validate type check user defined guard return narrow
Suggestion
const checkIsNumber = (value: unknown): value is number => typeof value === "string";
This function is invalid because the type specified in the return type (number
) does not match the reality at runtime (string
). However, TS does not currently throw a type error.
Could TypeScript type check user-defined type guards? Specifically it should check the narrowed type of value
in the function body (after all guards) matches the explicit return type.
const checkIsNumber = (value: unknown): value is number =>
typeof value === "string" &&
(() => {
// TS knows here that the type is narrowed to `string`.
// This does not match the explicit return type of this user-defined type guard (`number`).
// Therefore, TS could/should error?
value;
return typeof value === "string";
})();
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Documentation - Narrowing - TypeScript
It looks at these special checks (called type guards) and assignments, and the process of refining types to more specific types than declared...
Read more >PEP 647 – User-Defined Type Guards
This PEP specifies a way for programs to influence conditional type narrowing employed by a type checker based on runtime checks.
Read more >How to use type guards in TypeScript - LogRocket Blog
Type guards are typically used for narrowing a type and are quite similar to feature detection, allowing you to detect the correct methods, ......
Read more >narrowing types via type guards and assertion functions - 2ality
Note that TypeScript doesn't care how we compute the result of a user-defined type guard. That gives us a lot of freedom w.r.t....
Read more >Narrowing in TypeScript Using Type Guards
A type guard is a special check that helps you narrow down a type. ... We can use the typeof operator from JavaScript...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@RyanCavanaugh
How about a mode for type guards that asks the compiler to figure out the type of the parameter :
The use case of this is when moving a check the compiler can accurately type to a utility function.
The compiler will determine the type of the specific parameter when the return of the function is true. Most of the machinery to do this is there, I think the one extension would be if the return is an expression, take the expression and figure out the type on the true case and the false case of the expression.
First time I realized type guards were as unsafe as
as
I was shocked, as a result “invented” something like what fp-ts getRefinement is.Something like
value is infer
would be nice 👍