Skip to content

Polymorphism doesn't work with mapped type args and strictFunctionTypes #27984

Closed
@diegohaz

Description

@diegohaz

TypeScript Version: 3.2.0-dev.20181019

Search Terms: readonly polymorphism mapped types interface extend

Code

interface Base<P> {
    method: (args: Readonly<P>) => void;
}

function fn(arg: Base<any>) { }

// It works
fn({} as Base<{ foo: string }>)

// Why error?
interface Child<P> extends Base<P> { }
fn({} as Child<{ foo: string }>)

Expected behavior:

No error

Actual behavior:

Error:

Argument of type 'Child<{ foo: string; }>' is not assignable to parameter of type 'Base<any>'.
  Types of property 'method' are incompatible.
    Type '(args: Readonly<{ foo: string; }>) => void' is not assignable to type '(args: Readonly<any>) => void'.
      Types of parameters 'args' and 'args' are incompatible.
        Type 'Readonly<any>' is not assignable to type 'Readonly<{ foo: string; }>'.
          Property 'foo' is missing in type 'Readonly<any>'.

Playground Link:
Enable strictFunctionTypes in Options:
https://www.typescriptlang.org/play/index.html#src=interface%20Base%3CP%3E%20%7B%0D%0A%20%20%20%20method%3A%20(args%3A%20Readonly%3CP%3E)%20%3D%3E%20void%3B%0D%0A%7D%0D%0A%0D%0Afunction%20fn(arg%3A%20Base%3Cany%3E)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20It%20works%0D%0Afn(%7B%7D%20as%20Base%3C%7B%20foo%3A%20string%20%7D%3E)%0D%0A%0D%0A%2F%2F%20Why%20error%3F%0D%0Ainterface%20Child%3CP%3E%20extends%20Base%3CP%3E%20%7B%20%7D%0D%0Afn(%7B%7D%20as%20Child%3C%7B%20foo%3A%20string%20%7D%3E)

Related Issues:
Maybe related to #27484

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions