Skip to content

Inline user defined type guard #41710

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

Closed
5 tasks done
nandi95 opened this issue Nov 27, 2020 · 3 comments
Closed
5 tasks done

Inline user defined type guard #41710

nandi95 opened this issue Nov 27, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@nandi95
Copy link

nandi95 commented Nov 27, 2020

Search Terms

  • inline typeguard
  • user defined guard
  • if statement

Suggestion

In the logic flow there are times when typescript might not pick up the correct expected type. In that instances we can hint to the compiler by using value as MyType. However this as keyword only applies to that single use of value.

In a logic flow if I have an if statement that throws an error if it isn't an expected type then after that I should be safely be able to use value as MyType knowing that an error would have been thrown otherwise. See example 1. This is already possible with the use of dedicated methods with type guard return types but it would be nice to do this inline.
I imagine the typing would be defined as seen in example 2.

Use Cases

If the user only has one instance of using this inline typeguard they wouldn't need to extract into a separate function to get the guarding which ultimately also emits extra code.
Also if they choose not to extract then their only aternative is to use the as keyword on every reference to value or re-cast the value by value = value as MyType which I imagine will output an actual self-assignment in javascript.

Examples

Example 1 (playground link):

const obj: Record<string, any> = {
    'key': 'value'
};

if (obj !== null && typeof obj === 'object' && !Array.isArray(obj)) {
    throw new TypeError('err...');
}

console.log(obj.key); // Error, compiler not sure if `key` is available

Example 2:

type MyType = { key: string };

const obj: Record<string, any> = {
    'key': 'value'
};

if (obj !== null && typeof obj === 'object' && !Array.isArray(obj)): obj is MyType {
    throw new TypeError('err...');
}

console.log(obj.key); // Valid, compiler knows `MyType` has a `key` attribute

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.
@jcalz
Copy link
Contributor

jcalz commented Dec 1, 2020

Duplicate of #6474

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 1, 2020
@RyanCavanaugh
Copy link
Member

Also close to #10421

@nandi95
Copy link
Author

nandi95 commented Jan 11, 2021

Also close to #29188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants