You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The addition of as in mapped index types is a new feature of Typescript 4.1
It allows you to transform a mapped type like P in keyof T into a new type, for any given P in this case
One of the features of this is if you transform P into never, it acts as a filter!
So here, we can actually think of T[P] not as a union type, but rather as an individual instance of what T[P] resolves to for a given P. If T[P] extends Required<T>[P] for a given P, we know for sure the ? operator was not present on this key. Otherwise, the ? was present, so we can filter out the key P by mapping it to never
Hope this helped!
Reacted by Sun, Zach Guo, Jason Zhang, 王金平, Konstantinos Stratis, Eric Moore, Sliver and sciencefunq
hi @Dsan10s
I don't understand why this code get wrong?
as you explained, as allows me to transform k in keyof T into a new type, but when I add the brackets, It throw lots of error
hi @Dsan10s I don't understand why this code get wrong? as you explained, as allows me to transform k in keyof T into a new type, but when I add the brackets, It throw lots of error
I think this just a grammar limit. In this situation, keyword as and Key type can't be separated by bracket.
type GetRequired = { [K in keyof O as undefined extends O[K] ? never : K]: O[K] };
The non-required attribute will have undefined, and you can filter by using as to determine undefined extends O[K]
It is a wrong solution,since required properties may be union type including undefined. for example:
Expect<Equal<GetRequired<{ foo: undefined, bar?: undefined }>, { foo: undefined }>>,
Activity
Luncher commentedon Mar 13, 2021
hi @note89 Can you explain the function of as keyword here?
T[P]is union type,Why don't need to disable distribution ?thanks
Dsan10s commentedon Apr 13, 2021
Hi @Luncher , maybe I might be able to help
The addition of
asin mapped index types is a new feature of Typescript 4.1It allows you to transform a mapped type like
P in keyof Tinto a new type, for any givenPin this caseOne of the features of this is if you transform
Pintonever, it acts as a filter!So here, we can actually think of
T[P]not as a union type, but rather as an individual instance of whatT[P]resolves to for a givenP. IfT[P] extends Required<T>[P]for a givenP, we know for sure the?operator was not present on this key. Otherwise, the?was present, so we can filter out the keyPby mapping it toneverHope this helped!
Luncher commentedon Apr 14, 2021
@Dsan10s Thank you for your patient reply.. 👍
wine-fall commentedon Feb 16, 2022
hi @Dsan10s

I don't understand why this code get wrong?
as you explained,
asallows me to transformk in keyof Tinto a new type, but when I add the brackets, It throw lots of errortjx666 commentedon Mar 9, 2022
I think this just a grammar limit. In this situation, keyword
asand Key type can't be separated by bracket.dimitropoulos commentedon Jan 16, 2023
Thanks for the discussion here, everyone! I'll be featuring this solution and some of the things you said in the video solutions!
tomina1 commentedon Feb 20, 2024
The non-required attribute will have undefined, and you can filter by using as to determine undefined extends O[K]
skeleton1231 commentedon Aug 22, 2024
type GetRequired = {
[K in keyof T as T extends Record<K, T[K]> ? K : never]: T[K]
};
sciencefunq commentedon Aug 12, 2025
It is a wrong solution,since required properties may be union type including undefined. for example:
Expect<Equal<GetRequired<{ foo: undefined, bar?: undefined }>, { foo: undefined }>>,
ingadi commentedon Oct 30, 2025