Consider the following code, note the documentation of someField:
interface Test {
/**
* Some description.
* lorum ipsum.
* foo bar.
*/
someField: string;
}
function withGeneric<T extends Test>(arg: T) {
return arg;
}
function withoutGeneric(arg: Test) {
return arg;
}
withGeneric({
someField: ""
});
withoutGeneric({
someField: ""
});
When I hover with my mouse over someField in the withoutGeneric call, VSCode nicely shows me the documentation:
However, when hovering over someField in the withGeneric call, the documentation is not shown:
Is there a way to make the tsdoc documentation work with the generic argument too?



Consider that when you create the inline object, it is not typed as
Test:Your
withoutGenericfunction, by definition, takes aTest-typed argument, so TypeScript can infer the property types. However, when your function is generic but you don't explicitly set the generic type then TypeScript will infer the generic type from the inline object's type, which is just the default inferred type you can see above.You can see this in the intellisense:
TypeScript is inferring the generic type as
{ someField: string; }. This satisfies the requirements of theTestinterface, but the value is its own inferred default type, notTest.Consider the following example:
You get the following intellisense:
Note that the
withGenericfunction is satisfied by theFoobartype because it has the required properties and they are of the correct type. However, the doc string associated withsomeFieldwould beUnrelated doc stringbecause that is the type of theconfigvariable.If you explicitly set the generic type of your function, then it will behave as you expected.
Yields the following:
If you explicitly set the type to
Foobar, then you'll get Foobar's doc strings:yields: