You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is a bug or expected behavior, but I cannot find the corresponding documentation or related issues, if there is a duplicate issue of this, please let me know, thank you!
🔎 Search Terms
Discriminating unions
function type annotation
🕗 Version & Regression Information
I switched between all versions available in the playground and it seems to be the same behavior.
typeResult=Loading|Loaded;typeFunctionResult=()=>Result;constnonWorkingVersion: FunctionResult=()=>{// pass// return {// state: 'loaded',// data: 'data',// }// pass// return {// state: 'loading',// }// no compilation error (but I expect to receive compile error)return{state: 'loading',data: 'data',}}
🙁 Actual behavior
For the non-working version example I put above, it doesn't show any compile error even though the returned structure doesn't match any possibility of my discriminated union type Result.
🙂 Expected behavior
I expect the non-working version (which function type annotation is used) should work the same as working version example I put above.
The text was updated successfully, but these errors were encountered:
Additionally, this behaviour is unrelated to unions.
type Result = { a: string }
type GetResult = () => Result
const getResult: GetResult = () => ({ a: '', b: '' })
No return type is provided for the anonymous function, so it is inferred. In this case it's inferred to be { a: string, b: string }, and the type of the function is () => { a: string, b: string }. When assigning this to the variable the compiler will check if this type is assignable to GetResult, which it is: No properties are missing or incompatible.
You can get the behaviour you want by adding at type annotation to your function:
const getResult: GetResult = (): Result => ({ a: '', b: '' }) // Now an error
Bug Report
I'm not sure if this is a bug or expected behavior, but I cannot find the corresponding documentation or related issues, if there is a duplicate issue of this, please let me know, thank you!
🔎 Search Terms
🕗 Version & Regression Information
I switched between all versions available in the playground and it seems to be the same behavior.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
For the non-working version example I put above, it doesn't show any compile error even though the returned structure doesn't match any possibility of my discriminated union type
Result
.🙂 Expected behavior
I expect the non-working version (which function type annotation is used) should work the same as working version example I put above.
The text was updated successfully, but these errors were encountered: