Skip to content

Index signatures in mapped types override property types #12665

@zamb3zi

Description

@zamb3zi

TypeScript Version: nightly (2.2.0-dev.20161204)

Code

type T1 = {a?: number};
let o1: T1 = {a: 'no'}; // error: Type '{ a: string; }' is not assignable to type 'T1'
let p1: Partial<T1> = {a: 'no'}; // same error

type T2 = {a?: number, [key: string]: any};
let o2: T2 = {a: 'no'}; // error: Type '{ a: string; }' is not assignable to type 'T2'
let p2: Partial<T2> = {a: 'no'}; // ok - expected same error
let p3: {[P in keyof T2]: T2[P]} = {a: 'no'}; // also ok - expected same error

Expected behavior:

Applying Partial<T> to a type T that has an index signature should not modify the expected property types. The issue is the same regardless of whether the properties of T are optional or not and for the identity type mapping {[P in keyof T]: T[P]}.

The problem seems to be that the type mapping elevates the index signature to a higher priority in the mapped type than in the original type.

Actual behavior:

Partial<T & {[key: string]: any}> is identical to {[key: string]: any} for all types T, making Partial useless for types that have an index signature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions