Skip to content

String literal type unexpectedly widened to stringΒ #54767

Closed as not planned
Closed as not planned
@mspiess

Description

@mspiess

Bug Report

πŸ”Ž Search Terms

  • string literal type
  • widen
  • not assignable to

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about mapped types

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type StringKeyOf<T> = Exclude<keyof T, symbol>;
type JsonPointer<T> = T extends object
  ? { [K in StringKeyOf<T>]: `/${K}${JsonPointer<T[K]>}` | `/${K}` }[StringKeyOf<T>]
  : '';

function createJsonPointer<T>(key: StringKeyOf<T>): JsonPointer<T> {
    return `/${key}`;
}

πŸ™ Actual behavior

Following error is emitted because StringKeyOf<T> is falsely widened to string:

Type '`/${string}`' is not assignable to type 'JsonPointer<T>'.

πŸ™‚ Expected behavior

I expected the code to type check as it does if the function's type parameter is replaced with a concrete type.

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