We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
user-defined type guards
narrowing
discriminated union
Playground link with relevant code
type FooBar<T = 'foo' | 'bar'> = T extends string ? { type: T; value: Capitalize<T> } : never; declare const fooBar: FooBar; const isFoo = (type: 'foo' | 'bar'): type is 'foo' => type === 'foo'; if (fooBar.type === 'foo') { // 'Foo' fooBar.value } if (isFoo(fooBar.type)) { // 'Foo' | 'Bar' ??? fooBar.value }
typeof fooBar.value in the second if block is 'Foo' | 'Bar'
fooBar.value
if
'Foo' | 'Bar'
typeof fooBar.value in the second if block should be 'Foo' instead of 'Foo' | 'Bar'
'Foo'
The text was updated successfully, but these errors were encountered:
Duplicate of #42384.
You can work around this issue by narrowing the object, not the property:
const isFoo = (val: FooBar): val is FooBar<'foo'> => val.type === 'foo';
Sorry, something went wrong.
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
No branches or pull requests
Bug Report
π Search Terms
user-defined type guards
narrowing
discriminated union
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
typeof
fooBar.value
in the secondif
block is'Foo' | 'Bar'
π Expected behavior
typeof
fooBar.value
in the secondif
block should be'Foo'
instead of'Foo' | 'Bar'
The text was updated successfully, but these errors were encountered: