Skip to content

Indirect mapped type drops property modifiers #30330

Closed
@matt-tingen

Description

@matt-tingen

TypeScript Version: 3.4.0-dev.20190311

Search Terms: mapped modifier indirect optional readonly

Property modifies only seem to be copied in mapped types if the mapping is performed directly.

This seems to be a duplicate of #13724, but that issue was marked "Working as Intended" (for removing modifiers) around the release of 2.8 when the explicit syntax was added for adding/removing property modifiers. I was wondering if that conclusion would be reconsidered since there is a more explicit way of achieving that. I do understand that changing this behavior could be a potentially troublesome breaking change, though.

This came up in DefinitelyTyped/DefinitelyTyped#33742 as part of the implementation of React's JSX.LibraryManagedAttributes.

Code

type Foo = { bar?: string }
type Keys = keyof Foo

// okay; bar is optional
const direct: { [K in keyof Foo]: Foo[K] } = {}

// error; bar is required
const parentheses: { [K in (keyof Foo)]: Foo[K] } = {}
const indirect: { [K in Keys]: Foo[K] } = {}
const literal: { [K in 'bar']: Foo[K] } = {}

Expected behavior:
Creating a mapped type over the exact keys will copy property modifiers, resulting in no errors on any of the last 3 lines.

Actual behavior:
All 3 assignments error with Property 'test' is missing in type '{}' but required in type '{ test: string; }'.

Playground Link: Playground

Related Issues:
#13724 #28483 #24679

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions