Closed
Description
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
- 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.
⏯ Playground Link
Playground link with relevant code
💻 Code
- Working version
type Loading = {
state: 'loading';
};
type Loaded = {
state: 'loaded';
data: string;
};
type Result = Loading | Loaded;
function workingVersion(): Result {
// pass
// return {
// state: 'loaded',
// data: 'data',
// }
// pass
// return {
// state: 'loading',
// }
// compile error (as expected)
return {
state: 'loading',
data: 'data',
}
}
- Non-working version
type Result = Loading | Loaded;
type FunctionResult = () => Result;
const nonWorkingVersion: 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.
Metadata
Metadata
Assignees
Labels
No labels