interface Model {
a: string | null;
}
interface ModelA {
a: string;
}
function myFunc(m: ModelA) {
console.log(m.a.length);
}
function ModelFactory(): Model {
return { a: null };
}
const model = ModelFactory();
if (model.a) {
console.log(model.a.length);
myFunc({a:"s"});
myFunc(model); // Errors
}
TypeScript playground link.
I know I can write a user-defined typeguard or use " as ModelA" but both feel like sidestepping the type system. Any reason why it can't use the if type guard and conclude the second call to myFunc is also typesafe?