Skip to content

allow a flag that turns off covariant parameters when checking function assignability  #6102

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

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions