Closed
Description
Currently the following flawed code is permitted:
interface A { x: nuber; }
interface B { x: number, y: string }
function copyB(value: B) : B {
return { x: value.x, y: value.y };
}
let values : A[] = [];
let copied= values.map(copyB); // <-- no problem compiling, but a problem at runtime
Thank to the following line: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/checker.ts#L5654
I suggest to give developers a flag that would disable such unsound behavior allowing function subtyping the way it is supposed to be: https://en.wikipedia.org/wiki/Subtyping#Function_types.
An outline of a pull request of how this might look like: https://github.com/aleksey-bykov/TypeScript/commit/34b8f9b5a937cb7bb471771d5c9b9c4d4f629fc8
Related issues: #5961, #3523, #4895, #5741, #3067, #222, #5673
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment