question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better propagation of type predicate signatures

See original GitHub issue
declare 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:open
  • Created 7 years ago
  • Reactions:7
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
gcnewcommented, Dec 28, 2016

@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

2reactions
ascott18commented, Feb 24, 2021

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

class Extension { }
class ImageExtension extends Extension {
  imageUrl: string | null = null;
}

const extensions: Extension[] = [];
extensions.push(new ImageExtension());

// Doesn't work
const imageExtension: ImageExtension | undefined = extensions.find(e => e instanceof ImageExtension);

// Does work, but is unnecessarily verbose
const imageExtension2: ImageExtension | undefined = extensions.find((e): e is ImageExtension => e instanceof ImageExtension);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found