In the code snippet below, I have defined a type 'Something' which can have three possible values: 'A', 'B', or 'C'. The function 'f' takes an object of type Something as input and returns a string based on the value of 't'.
type Something =
| { t: 'A', src: string }
| { t: 'B', src: string }
| { t: 'C', src: number };
const f = (o: Something): string =>
o.t === 1
? o.src
: 'a-string';
I am puzzled by the fact that no error is generated when o.src
is returned for o.t
equal to 1 because the type 't' was never defined to be a number. This inconsistency in behavior raises questions about Flow's type checking capabilities and its handling of undefined or unreachable code segments.
To further explore this issue, you can use the following links for direct testing:
- Flow Playground Link
- TypeScript Version
The underlying question here revolves around the potential errors and warnings that static type checking systems like Flow should ideally catch when encountering unexpected data types within defined structures.