Closed
Description
TypeScript Version: 2.8.1
Search Terms:
strict interface return check
Code
interface A {
firstName: string
lastName: string
}
interface B {
firstName: string
}
const worksButMaybeShouldnt = (a: A): B => a // I expected an error here but none occurs
const correctlyFails = (a: A): B => ({...a}) // I expected this error
Expected behavior:
I expected the worksButMaybeShouldnt
function to have a compile error. It returns an object that has a different shape to the expected return type (it has an extra property: lastName
). The correctlyFails
example does something similar, effectively returning a copy of the input, and this correctly exhibits a compile error.
I would expect that the above 2 examples would both have compile errors.
Actual behavior:
There is no compile error at all and I'm able to return an object with a different shape (a superset of the return type).
Related Issues: