Skip to content

Intersection type kills mapped types restriction #27143

Closed
@luxalpa

Description

@luxalpa

TypeScript Version: [email protected] (also happens in 2.7.2)
tested with unmodified tsconfig generated by tsc --init

Search Terms: intersection mapped types restrictions

Code

type locales = 'us' | 'jp' | 'nz'

type YZ = {
  [routeName: string]: {someRandomProperty?:string} & { // this does not throw the error!
  // [routeName: string]: {} & {        // this will throw the error!
  // [routeName: string]: {             // just like this!
  // [routeName: string]: undefined & { // or this!
    urls: {
      [P in locales]?: string;
    }
  }
}

let value: YZ = {
  'hello': {
    urls: {
      jp: 'dragon',
      ph: 'unknown'
    }
  }
};

Expected behavior:
There should be an error message along the lines of Type '{ jp: string; ph: string; }' is not assignable to type '{ us?: string | undefined; jp?: string | undefined; nz?: string | undefined; }'. Object literal may only specify known properties, and 'ph' does not exist in type '{ us?: string | undefined; jp?: string | undefined; nz?: string | undefined; }'

Actual behavior:
No error message is thrown :(

Playground Link:
Playground never shows the error message for some reason.

Related Issues:

#19927
#22255

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions