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.

the "is" keyword (type predicates)

See original GitHub issue

To allow for type refinement to be abstracted into functions:

function isNumber(n: unknown): n is number {
  if (typeof n === 'number' && !isNaN(n)) return true
  return false
}

Try

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
thecotnecommented, May 21, 2020

@JSMonk you should allow developer to specify what he/she wants to refine to so refinement itself can be checked against expectation of a developer

for example

function isNumber<T>(n: T): $Refine<T, number>  {
  return typeof n === 'string'// error
}

function isString<T>(n: T): $Refine<T, string> {
  return typeof n === 'string'// ok
}

declare class Array {
  static isArray<T>(T): $Refine<T, Array>
}

of course inference of $Refine would be super cool but developer needs to be able to specify intent

1reaction
trusktrcommented, May 22, 2020

That’s a good point. It could be possible to make a mistake in the return statement, and therefore by accident change the type guard that the boolean represents.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does the `is` keyword do in typescript? - Stack Overflow
A type predicate is just used in compile time. The resulting .js file (runtime) will have no difference because it does not consider...
Read more >
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 >
Aha! Understanding Typescript's Type Predicates
Type predicates are a special return type that signals to the Typescript compiler what type a particular value is. Type predicates are always ......
Read more >
What is this return type with "is" keyword. : r/typescript - Reddit
It's called Type Predicate and is used in the context narrowing down the type of a value. For your example it'd mean that...
Read more >
Type predicate expressions and 'this' type assertions #12942
I'll try to break down what is happening here: The SameKeys type contains a type expression that evaluates to either the true or...
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