You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceA{firstName: stringlastName: string}interfaceB{firstName: string}constworksButMaybeShouldnt=(a: A): B=>a// I expected an error here but none occursconstcorrectlyFails=(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).
Duplicate of #12936 -- by default all types in TypeScript support subtyping and there's no way to turn it off. There's an exception for object literals since if you write const o: A = { ... } we assume you only intend to write properties in A, but not for variables.
You can circumvent the known property checks on spread properties, whether inadvertently or not, using the above. I imagine the above are intended to have the same behaviour.
TypeScript Version: 2.8.1
Search Terms:
strict interface return check
Code
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
). ThecorrectlyFails
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).
Playground Link: http://www.typescriptlang.org/play/#src=interface%20A%20%7B%0D%0A%20%20%20%20firstName%3A%20string%0D%0A%20%20%20%20lastName%3A%20string%0D%0A%7D%0D%0A%0D%0Ainterface%20B%20%7B%0D%0A%20%20%20%20firstName%3A%20string%0D%0A%7D%0D%0A%0D%0Aconst%20worksButMaybeShouldnt%20%3D%20(a%3A%20A)%3A%20B%20%3D%3E%20a%20%2F%2F%20I%20expected%20an%20error%20here%20but%20none%20occurs%0D%0A%0D%0Aconst%20correctlyFails%20%3D%20(a%3A%20A)%3A%20B%20%3D%3E%20(%7B...a%7D)%20%2F%2F%20I%20expected%20this%20error%0D%0A
Related Issues:
#3823
#391
The text was updated successfully, but these errors were encountered: