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:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types
Try [T] extends [AKeys] instead
wow, amazing, it's working.
Thank you for your help!
Most helpful comment
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types
Try
[T] extends [AKeys]instead