I've been working with TypeScript quite a bit lately and stumbled across a problem that is confusing to me. I've put the problem into an example which looks like the following:
type Options<T> = {
props: T;
g: (x: T) => void;
};
function f<T>(options: Options<T>) {
console.log(options);
}
// Example 1 (x and props expected type)
f({
props: {
foo: {
a: 200,
bar: () => {}
}
},
g(x) {
// x is the expected type
console.log(x)
}
});
// Example 2 (x and props unknown)
f({
props: {
foo: {
a: 100,
bar: function() {}
}
},
g(x) {
// x is unknown
console.log(x)
}
});
When hovering over props in the first example, you will see that it has the expected type. And although barely anything changed in the second example props is of type unknown. Why is that? Here is the example above in TS Playground.