I have a very simple code:
const foo = (state: RootState): MyData => undefined;
This gives an error:
Type 'undefined' is not assignable to type 'MyData'.
Which is quite reasonable since my type MyData
does not allow undefined.
But if I now write this:
const foo = (state: RootState): MyData => state.data?.myData;
Then it compiles without troubles. I don't understand what is going on here since if state.data
is undefined then it should be really returning undefined and should be quite obvious to the compiler.
Am I missing something here?
P.S. This is the minimal example for types:
type State = {
data: {
myData: MyData;
};
}
type MyData = {
}
RootState
andMyData
. If I make reasonable guesses, TypeScript complains about both functions.