Bug Report
When modifying a variable with a type Union of objects, the inferred type should be the Union of the possible results.
🔎 Search Terms
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Union and narrow
⏯ Playground Link
Playground link with relevant code
💻 Code
typeSource={type: 'ws'reader: {state: 'connecting'}}|{type: 'file'reader: {state: number}}letsource: Source={type: 'file',reader: {state: 12}}/* * Error: getSourceWithState return type is * { * type: "ws" | "file"; * state: "connecting" | number; * } * instead of * { * type: "file"; * state: number; * } | * { * type: "ws"; * state: "connecting"; * } */constgetSourceWithState=()=>{return{type: source.type,state: source.reader.state}}constsourceWithState=getSourceWithState()if(sourceWithState.type==='file'){// ErrorsourceWithState.state+=1}🙁 Actual behavior
The return type of getSourceWithState is
{
type: "ws"|"file";
state: "connecting"|number;}Then trying to narrow the type is impossible.
🙂 Expected behavior
The return type of getSourceWithState should be
{
type: "file";
state: number;}|{
type: "ws";
state: "connecting";}This would make it possible to narrow the type and is also the truth.
Bug Report
When modifying a variable with a type Union of objects, the inferred type should be the Union of the possible results.
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The return type of getSourceWithState is
Then trying to narrow the type is impossible.
🙂 Expected behavior
The return type of getSourceWithState should be
This would make it possible to narrow the type and is also the truth.