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

extends keyof does not allow extended new keys on interfaces since 4.3.0 #44295

Closed
posva opened this issue May 27, 2021 · 2 comments
Closed

extends keyof does not allow extended new keys on interfaces since 4.3.0 #44295

posva opened this issue May 27, 2021 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@posva
Copy link

posva commented May 27, 2021

Bug Report

πŸ”Ž Search Terms

  • extends keyof, missing key

πŸ•— Version & Regression Information

I saw this in 4.3.0 beta, it seems to exists in the nightly. It was working on 4.2.x

⏯ Playground Link

export interface GlobalOptions {
  // cannot be added or it wouldn't be able to be extended
  // suffix: string
}

// this is added by a user to extend a global customization
export interface GlobalOptions {
   // suffix: 'New'
}

export type WithSuffix<S extends string> = `${S}${'suffix' extends keyof GlobalOptions ? GlobalOptions['suffix'] : 'Suffix'}`

declare let a: WithSuffix<'hi'>
Output
export {};
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,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided


### πŸ™ Actual behavior

Error with `suffix cannot be used to index`

### πŸ™‚ Expected behavior

No Error
@posva posva changed the title extends keyof does not allow extended new keys on interfaces extends keyof does not allow extended new keys on interfaces since 4.3.0 May 31, 2021
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 2, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 2, 2021
@RyanCavanaugh
Copy link
Member

The idiomatic way to write this would be

export type WithSuffix<S extends string> = `${S}${GlobalOptions extends Record<"suffix", unknown> ? GlobalOptions['suffix'] : 'Suffix'}`

which works as expected.

The "narrowing" of the RHS in that position was a bug and shouldn't have happened.

@RyanCavanaugh RyanCavanaugh removed the Bug A bug in TypeScript label Jun 2, 2021
@RyanCavanaugh RyanCavanaugh removed this from the Backlog milestone Jun 2, 2021
@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 2, 2021
@posva
Copy link
Author

posva commented Jun 3, 2021

Thanks a lot for telling me the correct way of doing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants