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
There is a bug in Typescript related to keyof Array that affects ConditionalKeys<Base, Condition> and the Types using it. (reporting the bug itself to typescript, aswell)
It can be fixed adHoc without affecting any non.bugged existing uses
typeArr=readonly[0,1,2,3,4,5,6,7,8,9]typeWhoopMyArray_Faulty<BaseextendsArrayLike<any>,Condition>={[KeyinkeyofBase]:
Base[Key]extendsCondition
? 'Whoop it'
: Base[Key]extendsBase[number] ? Base[Key] : never}// action is performed on array elements but includes all values of Array.prototypetypeWhoopedArrayKeys=WhoopMyArray_Faulty<Arr,7|5|2>[keyofArr]
typeWhoopedArray=[0,1,'Whoop it',3,'Whoop it',5,'Whoop it',7,8,9]exporttypeConditionalKeys<Base,Condition>=NonNullable<// TODO: report bug in ts as type-fest// Wrap in `NonNullable` to strip away the `undefined` type from the produced union.{// Map through all the keys of the given base type. NOTE: dummy-prop is needed for Arrays[KeyinkeyofBase|'__im-a-dummy-prop__']:
// Pick only keys with types extending the given `Condition` type.Base[Exclude<Key,'__im-a-dummy-prop__'>]extendsCondition// Retain this key since the condition passes.
? Key// Discard this key since the condition fails.
: never// Convert the produced object into a union type of the keys which passed the conditional test.}[keyofBase]>;typeWhoopedArrayKeys=ConditionalKeys<WhoopedArray,'Whoop it'>// -> '2' | '4' | '6'
Took me ages to find out why my stuff isn't working
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
The funding will be given to active contributors.
Thank you in advance for helping prioritize & fund our backlog.
The text was updated successfully, but these errors were encountered:
exporttypeConditionalKeys<Base,Condition>={// Map through all the keys of the given base type.[KeyinExclude<keyofBase,never>]:
// Pick only keys with types extending the given `Condition` type.Base[Key]extendsCondition// Retain this key since the condition passes.
? Key// Discard this key since the condition fails.
: never;// Convert the produced object into a union type of the keys which passed the conditional test.}[keyofBase];
fregante
changed the title
Bug: ConditionalKeys<Base, Condition> fails when used on Arrays
ConditionalKeys<Base, Condition> fails when used on Arrays
Aug 13, 2024
As pointed out in the TS repo issue, keyof shouldn't be used on arrays, and I don't understand why you'd want to use ConditionalKeys on an array either as arrays don't have "keys" but "indexes"
There is a bug in Typescript related to
keyof Array
that affectsConditionalKeys<Base, Condition>
and the Types using it. (reporting the bug itself to typescript, aswell)It can be fixed adHoc without affecting any non.bugged existing uses
case:
Playground Link
So using
ConditionalKeys<Base, Condition>
Playground Link
results in
"2" | "4" | "6"
and all Array.prototype-valuesthis can be fixed by adding a temporary dummy key to the UtilityType:
Playground Link
Took me ages to find out why my stuff isn't working
Upvote & Fund
The text was updated successfully, but these errors were encountered: