Skip to content

Typescript 4.7 thinks keyof interface may contain a symbolΒ #49261

Closed
@bschlenk

Description

@bschlenk

Bug Report

πŸ”Ž Search Terms

Implicit conversion symbol fail runtime

πŸ•— Version & Regression Information

  • This changed between versions 4.6.* and 4.7.*

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Property<T> {
  name: keyof T
}

interface ThingA {
  a: string
  b: string
}

interface ThingB {
  c: string
  d: string
}

function Filter<T extends ThingA | ThingB>(properties: Property<T>[]) {
  return properties.map((property) => `input-${property.name}`) // <-- Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.(2731)
}

πŸ™ Actual behavior

Typescript thinks that property.name here may be a Symbol. I guess maybe this makes sense since T extends ThingA | ThingB wouldn't prohibit a ThingC interface that extends ThingA with a symbol property. But I'd expect this new error to pop up in a lot more places in my codebase, but I only see it in this one spot.

I'm not sure in what circumstances this error is triggered, or how to work around it. I'd rather not add String(...) around the property because that's unnecessary extra runtime code.

πŸ™‚ Expected behavior

Because this doesn't fail in TS < 4.7, I'd expect it not to suddenly start failing.

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