Skip to content

Mapped type beyond one level simplification problem with Partial and Record. #29752

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

Closed
bowenni opened this issue Feb 5, 2019 · 1 comment
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@bowenni
Copy link

bowenni commented Feb 5, 2019

TypeScript Version: 3.4.0-dev.20190202

Search Terms:
Record, Partial

Code

function f<K extends string, V, T extends Partial<Record<K, V[]>>>(
    object: T,
    key: K,
    value: V[],
) {
  object[key] = value;
}

Expected behavior:
No errors.

Actual behavior:
index.ts:6:3 - error TS2322: Type 'V[]' is not assignable to type 'T[K]'.

Playground Link:
https://www.typescriptlang.org/play/#src=function%20f%3CK%20extends%20string%2C%20V%2C%20T%20extends%20Partial%3CRecord%3CK%2C%20V%5B%5D%3E%3E%3E(%0D%0A%20%20%20%20object%3A%20T%2C%0D%0A%20%20%20%20key%3A%20K%2C%0D%0A%20%20%20%20value%3A%20V%5B%5D%2C%0D%0A)%20%7B%0D%0A%20%20object%5Bkey%5D%20%3D%20value%3B%0D%0A%7D
Enabling all strictness flags still gives me the same error.

Related Issues:
I think this is a duplicate of #28839, which is marked as fixed. I asked for reopen in #28839 (comment) but got no rely. I'm assuming that closed issues are not on your radar so opening a new issue.

@ahejlsberg
Copy link
Member

This is working as intended. In your example, a type argument for T can be a subtype of Partial<Record<K, V[]>>, so properties of T might actually be subtypes of V[] (e.g. a specialized array with additional properties). The type safe way to write your example is:

function f<K extends string, V>(
    object: Partial<Record<K, V[]>>,
    key: K,
    value: V[],
) {
  object[key] = value;
}

This compiles without errors.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants