Skip to content

[Bug] [keyof] Unexpected behavior of keyof of an interface with optional properties  #24679

Closed
@franklixuefei

Description

@franklixuefei

TypeScript Version: 2.9.1

Search Terms:
keyof,optional
Code

export type Map<T = {}> = {
  readonly [P in Extract<keyof T, string>]: A<P>;
};

export interface A<TNameType extends string = string> {
    test: string;
}

export interface B {
    required: string;
    optional?: string;
}

export const Test: Map<B> = {
    required: 'required',
}

Expected behavior:

No errors should be reported, because an optional property should not be required as a result of keyof.

Actual behavior:

Type '{ required: string; }' is not assignable to type 'Map<B>'.
  Property 'optional' is missing in type '{ required: string; }'.

It was working in 2.8.3, without Extract<>

Playground Link:
Playground Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions