Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tadhgmister opened this issue Aug 12, 2020 · 2 comments
Closed

Comments

@tadhgmister
Copy link

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

@jack-williams
Copy link
Collaborator

https://github.com/microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

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.

@tadhgmister
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants