microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.86k stars 12.36k forks source link

Suggest interpolated `number`, `string` and `bigint` types in TypeScript template literals #57545

Open ddeltree opened 6 months ago

ddeltree commented 6 months ago

Currently, if you create a variable of an interpolated string type using either number, string or bigint inside you'll get no suggestion for it: image

I think it would be more helpful to provide suggestions for those types of strings, having no apparent reason not to. Especially considering the type annotation could be a generic type, in which case you'd have to hover over the variable in order to be aware of the possible values.

JesusTheHun commented 3 months ago

In the current state, it is difficult to offer suggestions when the template includes another template.

Think about the following example as a type that represent the path to properties of a document in a database.

type UserId = `user::${number}`;
type MembershipPaths<T> = `membership.${T}.joinedAt` | `membership.${T}.leftAt` | `membership.${T}.invitedBy`;

type Group = {
  name: string;
  membership: Record<UserId, {
    joinedAt: number;
    leftAt?: number;
    invitedBy: UserId;
  }>
};

type GroupPaths = 'name' | MembershipPaths<UserId>;

As you can imagine, in reality the types are actually generated on the fly by passing the document type. In the case presented here, to provide a suggestion that is valid, one has to find the static part of the template T, which is not impossible, but requires some work.

Also, using this workaround, once the suggestion is selected, the dev has to remove the fake part of the suggestion.