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

Incorrect error when assigning to omitted and extended record type #27883

Closed
WearyMonkey opened this issue Oct 13, 2018 · 2 comments
Closed

Incorrect error when assigning to omitted and extended record type #27883

WearyMonkey opened this issue Oct 13, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types Domain: Mapped Types The issue relates to mapped types Fixed A PR has been merged for this issue

Comments

@WearyMonkey
Copy link

TypeScript Version:
[email protected]
[email protected]
[email protected]

Search Terms:

Code

type Baa = { a: number, b: number };
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

function foo<P extends Baa>() {
    const foo: { foo: string } & Omit<P, 'b'> = null as any;
    const props: Baa = foo;
}

Expected behavior:

No error.

Actual behavior:

test.ts:6:11 - error TS2322: Type '{ foo: string; } & Pick<P, Exclude<keyof P, "b">>' is not assignable to type 'Baa'.
  Property 'a' is missing in type '{ foo: string; } & Pick<P, Exclude<keyof P, "b">>'.

6     const props: Baa = foo;
            ~~~~~

Removing the { foo: string } & works.
Removing the Omit works.
Removing the P and using Baa directly works.

Playground Link:

https://agentcooper.github.io/typescript-play/?ts=2.9.1#code/C4TwDgpgBAQghnKBeKBvKcBcUB2BXAWwCMIAnAGiiIH5t9iyoBfAbgChRIoB5AgS2AAeACqUA0gD5kUAAp8AxgGsRlAKIAPeQBs8AEwiDFEEAHsAZlFFRJE9mzN4c84HxM4oZkycEyoEdcAQOLoAzrAIEgAUAJRobFAJUPJuIcAeXtjonibYqaR8OADmzFAAZDz8QjKUAORENVIo+FpaGGFwOCDsiUkpaWCkJmAh2PCIKNnsTGxAA

Related Issues:

@ghost
Copy link

ghost commented Oct 16, 2018

This isn't just a problem with the error message, but that we don't recognize Omit<P, 'b'> as having any properties at all.

function f<T extends Record<"a" | "b", any>>(x: Record<Exclude<keyof T, 'b'>, any>): { a: any } { return x; }

T's keys are at least "a" | "b", so Exclude<keyof T, "b"> contains at least "a", so x should have a as a key. I think the type system currently does not have a notion on a lower bound on a type -- we can specify that T extends something, but not T super something. So we can't understand that keyof T will contain "a" even when "b" is excluded.

@ghost ghost added the Bug A bug in TypeScript label Oct 16, 2018
@weswigham weswigham added the Domain: Mapped Types The issue relates to mapped types label Oct 16, 2018
@ghost ghost mentioned this issue Oct 18, 2018
@DanielRosenwasser DanielRosenwasser changed the title Incorrect error when assigning to omited and extended record type Incorrect error when assigning to omitted and extended record type Oct 25, 2018
@DanielRosenwasser DanielRosenwasser added the Domain: Conditional Types The issue relates to conditional types label Oct 25, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.2 milestone Oct 25, 2018
@ahejlsberg
Copy link
Member

Fixed by #29121.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Feb 1, 2019
@ahejlsberg ahejlsberg removed this from the TypeScript 3.4.0 milestone Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types Domain: Mapped Types The issue relates to mapped types Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants