-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Second use of type guard produces different result #52232
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
Comments
😱 Anyone interested in trying to produce a repro that doesn't need the entire React typings? |
Stellar, thank you! |
interface ReactElement<P> {
props: P;
}
interface SomeReactElementWithFoo extends ReactElement<any> {
foo: string;
}
type ReactNode = ReactElement<any> | SomeReactElementWithFoo;
declare function isValidElement<T>(object: unknown): object is ReactElement<T>;
function test(obj: ReactNode) {
const p1 = isValidElement(obj) && obj.props.baz;
const p2 = isValidElement(obj) && obj.props.baz;
} |
The change between origin/release-4.7 and origin/release-4.8 occurred at a4507c9. |
Root cause is #50044, cc @ahejlsberg Possible fixes:
|
Part of this issue is an effect of #50044. With that PR we consistently favor the asserted type in a type predicate narrowing, meaning that There isn't an error on the first property access is because the So, overall this is working as intended, though I'll admit it's subtle. |
BTW, I'll second @RyanCavanaugh's suggested fix:
|
Thanks for checking into this. I'll PR against React library to land a fix. |
I'm not quite willing to sign on to "intended" (lol) but given that the UDTG definition is extremely suspect I think we can call this "best we can do for now". Hopefully one day we can solve the any/unknown subtyping dilemma. |
Bug Report
🔎 Search Terms
react child.props unknown
ts 18046 reading child prop
typescript user defined type guard fails after second time
🕗 Version & Regression Information
Upgrading project to 4.9.4 from 4.2.4.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
All examples fail after the second read of a type guard on
props
, whether re-running the function or just reading the variable.🙂 Expected behavior
Should behave consistently no matter if this is the first or second read of the type guard.
The text was updated successfully, but these errors were encountered: