Better propagation of type predicate signatures
See original GitHub issuedeclare function isNumber(value: {}): value is number;
declare const values: {}[];
declare function filterAs<a, b extends a>(values: a[], isIt: (value: a) => value is b): b[];
const yayNumbers = filterAs(values, isNumber); // number[]
const ohNoNotAgain = filterAs(values, x => isNumber(x)); /* <-- expected to work,
actual: Argument of type '(x: {}) => boolean' is not assignable to parameter of type '(value: {}) => value is {}'.
Signature '(x: {}): boolean' must have a type predicate. */
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:18 (9 by maintainers)
Top Results From Across the Web
Documentation - Advanced Types - TypeScript
A predicate takes the form parameterName is Type , where parameterName must be the name of a parameter from the current function signature....
Read more >Is it impossible to have a function return a user-defined type ...
The answer is going to be that the compiler only infers boolean as assignable to a type predicate in the specific situation where...
Read more >From Monomorphic to Polymorphic Well-Typings and ... - Springer Link
have the same type signature as the predicate. Other calls, appearing higher in the call graph of the program have a type signature...
Read more >TypeScript — Make types “real”, the type guard functions
We will see that this is not a fatality, because TypeScript is more powerful than you thought and some developers of the community...
Read more >Announcing TypeScript 4.8 - Microsoft Developer Blogs
When it comes to types, you'll often either need to propagate constraints, or intersect your types with {} . For more information, you...
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
@aleksey-bykov Implementing features that are useful but not entirely thorough attracts a lot of criticism and is oftentimes even questionable. e.g. https://github.com/Microsoft/TypeScript/issues/13002
Another use case here that seems like it should work but doesn’t - direct type assertions in a predicate (as opposed to a type assertion being done by a subroutine of a predicate):
Playground