Skip to content

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

Closed

Description

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

Activity

  1. 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[/+] on May 31, 2021
  2. RyanCavanaugh commented on Jun 2, 2021

    @RyanCavanaugh
    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.

  3. posva commented on Jun 3, 2021

    @posva
    Author

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

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions