Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: check narrowed type in user-defined type guards #29980

Closed
4 of 5 tasks
OliverJAsh opened this issue Feb 19, 2019 · 8 comments
Closed
4 of 5 tasks

Suggestion: check narrowed type in user-defined type guards #29980

OliverJAsh opened this issue Feb 19, 2019 · 8 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Feb 19, 2019

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.
@OliverJAsh OliverJAsh changed the title Check return type in user-defined type guards Suggestion: check narrowed type in user-defined type guards Feb 20, 2019
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Feb 27, 2019
@RyanCavanaugh
Copy link
Member

For the most part we don't expect UDTGs to be checkable at all, because that's why the author is writing them in the first place. Trivial type guards which we would be able to check may as well be inlined anyway, and more error-prone type guards wouldn't be checkable anyway.

@OliverJAsh
Copy link
Contributor Author

that's why the author is writing them in the first place

Not necessarily—sometimes they're written just to abstract checks so we don't have to repeat them. Such is the case with checkIsObject:

const checkIsObject = (value: unknown): value is object =>
  typeof value === 'object' && object !== null;

@dragomirtitian
Copy link
Contributor

@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.

@OliverJAsh
Copy link
Contributor Author

OliverJAsh commented Nov 15, 2019

For anyone who wants to achieve this today, check out fp-ts Option.getRefinement function. Update: blog post https://unsplash.com/blog/user-defined-type-guards-not-safe/

@safareli
Copy link

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 👍

@Lonli-Lokli
Copy link

Why there is no validation for typeguards (playground ) ? From my understanding this code should fail in compile time

type WithA = { a?: { someMethod: (x: number) => number } } 

const thingWithA: WithA = { a: undefined };

const typeGuard = (obj: unknown): obj is WithA => {
    return typeof obj === 'object' && obj !== null && 'unknownProp' in obj;
}

@fdcds
Copy link

fdcds commented Nov 15, 2021

Why there is no validation for typeguards (playground ) ? From my understanding this code should fail in compile time

type WithA = { a?: { someMethod: (x: number) => number } } 

const thingWithA: WithA = { a: undefined };

const typeGuard = (obj: unknown): obj is WithA => {
    return typeof obj === 'object' && obj !== null && 'unknownProp' in obj;
}

I think your example fits the suggestion described in #21732.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Too Complex" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

7 participants