class Foo {
constructor(public contained : number) {}
}
type FooStruct = {contained: number};
function meth(x: Foo) {
return x.contained *= x.contained;
}
function match(arg: unknown) {
if (arg
&& typeof arg === "object"
&& 'contained' in arg // This line should be unnecessary anyways
&& typeof arg.contained === "number") {
meth(arg); // TS incorrectly flags this as an error
// Inferred type: arg: object & Record<'contained', unknown>
}
if (arg instanceof Foo) {
meth(arg); // OK
}
}
How to get TS to correctly infer the type for the first meth() invocation?
Playground link below