Search Terms
#tuple #mapped #as #clause #preserve
Suggestion
New mapped type's as clause allows one to filter the entries of an object. Basic mapped types preserve the shape of a tuple but not when used with the new as clause:
typetuple=[0,1,2]typeMapped<T>={[KinkeyofT]: T[K]|undefined}typeMappedAs<T>={[KinkeyofTasK]: T[K]|undefined}typetest0=Mapped<tuple>// tuple shape is preservedtypetest1=MappedAs<tuple>// tuple shape is object nowtest0===[0|undefined,1|undefined,2|undefined]test1==={[x: number]: 0|1|2|undefined;0: 0|undefined;1: 1|undefined;2: 2|undefined;
length: 3|undefined;
toString: (()=>string)|undefined;
toLocaleString: (()=>string)|undefined;
...
}Use Cases
- Filtering lists without having to rely on costly recursive types
Examples
typeFilterNumbers<T>={[KinkeyofTasT[K]extendsnumber ? never : K]: T[K]|undefined}typetest2=FilterNumbers<[1,'2',3,'4']>// ['2', '4']Checklist
My suggestion meets these guidelines:
Search Terms
#tuple #mapped #as #clause #preserve
Suggestion
New mapped type's
asclause allows one to filter the entries of an object. Basic mapped types preserve the shape of a tuple but not when used with the newasclause:Use Cases
Examples
Checklist
My suggestion meets these guidelines: