Closed
Description
TypeScript Version:
1.8.10
Code
interface Foo {
foo()
bar()
}
// does NOT error:
var a = <Foo> {
foo() {}
// missing bar() - no error!
}
var b = {
foo() {}
// missing bar() - no error!
} as Foo
// errors: (b not assignable to type Foo)
var c: Foo = {
foo() {}
// missing bar() - error
}
function d() : Foo {
return {
foo() {}
// missing bar() - error
}
}
In the two first cases, the shape (and inferred type) of the anonymous object are missing a method and clearly are not assignable to Foo
- I was expecting an error message.
I don't fully understand why assignment to a var vs assignment to an explicitly typed var or return-type would behave inconsistently in terms of type-checking.
Didn't it use to be that you had to do something like var a = <Foo> <any> { .. }
, e.g. you had to explicitly cast the inferred object type/shape to any
before you'd be allowed to do an unsafe type-cast?