Skip to content

Mapped types of primitives returns primitive directly ignoring mapping. #40012

Closed
@tadhgmister

Description

@tadhgmister

TypeScript Version: 3.3.3333

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.

Related Issues:
#29793
possibly related to #39838, #38001, #38646

Code

// these 2 types should be equivelent but don't seem to be, 
// when passed 'string', 'number', 'boolean' Breaks gives the same type back
type Breaks<T> = {[K in keyof T]: never}
type ShouldBeEquivelentTo<T> = Record<keyof T, never>;

// works as expected for non primatives
type Check1 = Breaks<{a:any, b: any}>
// this gives 'string' which is very much not correct...
type Is_equal_to_string = Breaks<string>

const shouldNotCompile: Breaks<string> = "how is a string assignable to {[keys]: never} ? "
const doesntCompile: ShouldBeEquivelentTo<string> = "this is correctly caught as error";
Output
"use strict";
const shouldNotCompile = "should not be assignable but is?";
const doesntCompile = "this is correctly caught as error";
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions