Home > Blockchain >  Typescript forward generic type definition
Typescript forward generic type definition

Time:02-01

consider this type and this generic:

type Data = { item: number };

type Generic<T> = {
  obj: T;
};

now this is an instance of it:

const test: Generic<Data> = { obj: { item: 0 } };

now what I really need when using test.obj is to see the type for obj (Data) when clicking on it, not it's type in the Generic (T)

When clicking on test.obj

enter image description here

What I expected:

to go to Data type definition

type Data = { item: number };

What happend:

it went to its definition in the generic T

type Generic<T> = {
  obj: T;
};

so is this possible?

CodePudding user response:

If I understand the question, "simply" don't annotate your variable.

If you really must and you can use TS 4.9, use satisfies instead:

const test = { obj: { item: 0 } } satisfies Generic<Data>;

Choose Go to Type Definition

result

  • Related