如何区分 Typescript 中的两种模板文字类型?

How To Get Difference Between Two Template Literal Types In Typescript?

给定以下示例片段:

const firstName = `Abraham` as const;
const fullName = `${firstName}Lincoln` as const;

type FullName = typeof fullName;
type FirstName = typeof firstName;

// type LastName = "Lincoln"

考虑到 firstName 已知并且 lastName 已经包含在 [=14] 中,目标是在不写出的情况下访问 LastName 类型=].

我试过 two similar approaches,none 有收获。

const firstName = `Abraham` as const;
const fullName = `${firstName}Lincoln` as const;

type FullName = typeof fullName;
type FirstName = typeof firstName;


type Difference<T extends string, U extends string> = 
  // T is the common part
  // Scd is the difference we want to retrieve
  U extends `${T}${infer Scd}` ? Scd : never

type Result = Difference<FirstName, FullName> // Lincoln

Playground