Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit is broken / Modifiers not inferred #31232

Closed
millsp opened this issue May 3, 2019 · 1 comment
Closed

Omit is broken / Modifiers not inferred #31232

millsp opened this issue May 3, 2019 · 1 comment

Comments

@millsp
Copy link
Contributor

millsp commented May 3, 2019

@DanielRosenwasser

The new version of Omit discards the field modifiers. The solution is to avoid using a type directly in a keyof statement as TS loses the inference of the field modifiers (optional, readonly).

I am not sure if it should behave this way, as you looked confident writing the Omit type, it's probably part of another problem. In short, TS discards the inference of field modifiers on mapped types that map with another type (instead of keyof).

TypeScript Version: 3.5.0-dev.20190501

Search Terms:
mapped type readonly lose modifiers

Code

type Omit<T, K extends keyof any> = {
    [P in Exclude<keyof T, K>]: T[P]
} //     ^^^^^^^^^^^^^^^^^^^^ optional & readonly not inferred

// Mapping with a type like Exclude<keyof T, K> caused TS to forget modifiers

type O = {
    readonly a: string
    b?: number
    c?: any
}

type test00 = Omit<O, 'b'>

type WhatWeGet = {
    a: string;
    c: any;
} // === test00

type WhatItShouldBe = {
    readonly a: string;
    c?: any;
} // !== test00

Expected behavior:
Should preserve the modifiers

Actual behavior:
Loses the modifiers on the fields

Playground Link:

Related Issues:

@jcalz
Copy link
Contributor

jcalz commented May 3, 2019

Duplicate of #31190

@millsp millsp closed this as completed May 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants