Typescript: Conditional type with Pick working is incorrect

Created on 17 Jan 2020  路  2Comments  路  Source: microsoft/TypeScript


TypeScript Version: 3.7.2


Search Terms:

conditional type, union, pick, intersection

Code

type A = { a: string, b: string, c: string }
type AKeys = keyof A
type ResultType<T extends AKeys | null> = T extends AKeys ? Pick<A, T> : []
type TestFunc = <T extends AKeys | null>(keys: T[]) => ResultType<T>

const test: TestFunc = (() => { }) as any
const result = test(['a', 'b'])
// For now this result become ` Pick<A, "a"> | Pick<A, "b">`

Expected behavior:
result type should be Pick<A, "a"> & Pick<A, "b"> or Pick<A, "a" | "b"> to be able to access to properties.

Actual behavior:
Pick<A, "a"> | Pick<A, "b">.

Playground Link:
https://www.typescriptlang.org/play/#code/C4TwDgpgBAglC8UDeUCGAuKBnYAnAlgHYDmANFAEaY4EnkDG1eRxUAvgFCiSwDSEILAigBrAQHsAZrC7hoAJQhYArgBtgAFTkAeDVAgAPYBEIATITH6CoAHyiE1qgHzC9h42YtWhAfigAFfHoRbRhyDRdMAG0AXVkeDSVgADFlQnphXX0jE3M+ASE7B1VnAAoxQUwNWIBKBBdFFXUtSF0nDg56cUIcKGMcKqTU9OFS0rr4FxQ2OtQhVEIQTu7e3CU1YGF+4FKogHJUPfI9ij2YmqA
Related Issues:

Most helpful comment

All 2 comments

wow, amazing, it's working.
Thank you for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

zhuravlikjb picture zhuravlikjb  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

wmaurer picture wmaurer  路  3Comments

manekinekko picture manekinekko  路  3Comments