-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Empty object in ternary operator produces invalid type #50171
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
Strings are valid |
why isn't this code simplified? type X = string | {} // `X` is string | {} |
Subtype reduction isn't required to happen, but is allowed to happen at any point. There's not a bug here. |
but do you think this type reduction is correct / meaningful to developers? |
I encountered this issue when composing a dynamic object. For anyone seeing this and trying to find a workaround, couple things to note :
(true ? 'I am a string' : {}) // Type is 'I am a string' | {}
const myVar = true ? 'I am a string' : {} // Type is {}
const computeData = () => ({ foo: "bar"})
const subtypeReduced = true ? computeData() : {}; // Type is {}
const preservedType = {...(true ? computeData() : {}) } // Type is { foo?: string | undefined; } |
Bug Report
π Search Terms
empty object, {}, inferred type
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Type of
x
is inferred as{}
π Expected behavior
Type of
x
should be inferred as"I am a string" | {}
.For example, if we try this code:
...the type of
x
is inferred as"I am a string" | 5
The text was updated successfully, but these errors were encountered: