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
Search Terms:
mapped types, [K in keyof T] , primitives, undefined, number, string, boolean, short circuit Expected behavior: {[K in keyof T]: V} should have identical behaviour to Record<keyof T, V> for any types T and V Actual behavior:
for primitive types or unions of them (undefined, string, number, or boolean) the {[K in keyof T]: V} just returns T ignoring V.
// these 2 types should be equivelent but don't seem to be, // when passed 'string', 'number', 'boolean' Breaks gives the same type backtypeBreaks<T>={[KinkeyofT]: never}typeShouldBeEquivelentTo<T>=Record<keyofT,never>;// works as expected for non primativestypeCheck1=Breaks<{a:any,b: any}>// this gives 'string' which is very much not correct...typeIs_equal_to_string=Breaks<string>constshouldNotCompile: Breaks<string>="how is a string assignable to {[keys]: never} ? "constdoesntCompile: ShouldBeEquivelentTo<string>="this is correctly caught as error";
Output
"use strict";constshouldNotCompile="should not be assignable but is?";constdoesntCompile="this is correctly caught as error";
This mapped type returns a primitive type, not an object type.
Mapped types declared as { [ K in keyof T ]: U } where T is a type parameter are known as homomorphic mapped types, which means that the mapped type is a structure preserving function of T. When type parameter T is instantiated with a primitive type the mapped type evaluates to the same primitive.
well I feel silly, II had misunderstood that when I initially read it and was so focused on tracking down the issue when I narrowed it down to this I forgot to check. Still seems odd but thanks.
TypeScript Version: 3.3.3333
Search Terms:
mapped types,
[K in keyof T]
, primitives,undefined
,number
,string
,boolean
, short circuitExpected behavior:
{[K in keyof T]: V}
should have identical behaviour toRecord<keyof T, V>
for any typesT
andV
Actual behavior:
for primitive types or unions of them (
undefined
,string
,number
, orboolean
) the{[K in keyof T]: V}
just returnsT
ignoring V.Related Issues:
#29793
possibly related to #39838, #38001, #38646
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: