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.

Suggestion: check narrowed type in user-defined type guards

See original GitHub issue

Search 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:open
  • Created 5 years ago
  • Reactions:10
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
dragomirtitiancommented, Jun 23, 2019

@RyanCavanaugh

How about a mode for type guards that asks the compiler to figure out the type of the parameter :

const isNumber = (value: unknown): value is  infer =>
  typeof value === 'number' 

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.

3reactions
safarelicommented, Apr 16, 2021

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 👍

Read more comments on GitHub >

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

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