简体   繁体   English

如何为具有扩展接口的泛型类型的函数参数做 TypeScript tsdoc 文档?

[英]How to do TypeScript tsdoc documentation for function argument with generic type that extends an interface?

Consider the following code, note the documentation of someField :考虑以下代码,注意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:当我悬停我的鼠标someFieldwithoutGeneric通话,VSCode很好地显示我的文档:

在此处输入图片说明

However, when hovering over someField in the withGeneric call, the documentation is not shown:但是,在withGeneric调用someField悬停在someField时,不会显示文档:

在此处输入图片说明

Is there a way to make the tsdoc documentation work with the generic argument too?有没有办法使 tsdoc 文档也与通用参数一起使用?

Consider that when you create the inline object, it is not typed as Test :考虑到当您创建内联对象时,它的类型不是Test

const config = {
    someField: ""
};

Your withoutGeneric function, by definition, takes a Test -typed argument, so TypeScript can infer the property types.根据定义,您的withoutGeneric函数采用Test类型的参数,因此 TypeScript 可以推断属性类型。 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.但是,当您的函数是泛型但您没有显式设置泛型类型时,TypeScript 将从内联对象的类型推断泛型类型,这只是您在上面看到的默认推断类型。

You can see this in the intellisense:您可以在智能感知中看到这一点:

TypeScript is inferring the generic type as { someField: string; } TypeScript 将泛型类型推断为{ someField: string; } { someField: string; } . { someField: string; } . This satisfies the requirements of the Test interface, but the value is its own inferred default type, not Test .这满足了Test接口的要求,但是值是它自己推断的默认类型,而不是Test

Consider the following example:考虑以下示例:

interface Foobar {
    /**
     * Unrelated doc string
     */
    someField: string;
}

const config:Foobar = {
    someField: ""
}

You get the following intellisense:您将获得以下智能感知:

Note that the withGeneric function is satisfied by the Foobar type because it has the required properties and they are of the correct type.请注意, Foobar类型满足withGeneric函数,因为它具有所需的属性并且它们是正确的类型。 However, the doc string associated with someField would be Unrelated doc string because that is the type of the config variable.但是,与someField关联的文档字符串将是Unrelated doc string因为这是config变量的类型。

If you explicitly set the generic type of your function, then it will behave as you expected.如果您显式设置函数的泛型类型,那么它会按您的预期运行。

withGeneric<Test>({
    someField: ""
});

Yields the following:产生以下结果:

If you explicitly set the type to Foobar , then you'll get Foobar's doc strings:如果您明确地将类型设置为Foobar ,那么您将获得 Foobar 的文档字符串:

withGeneric<Foobar>({
    someField: ""
});

yields:产量:

I found an answer that works for my purpose.我找到了一个适合我的目的的答案。 Add a union with the extended from type:添加具有扩展自类型的联合:

/**
 * withGeneric sample
 * @typeParam T - must be a Test
 * @param arg 
 * @returns 
 */
function withGeneric<T extends Test>(arg: T & Test) {
    return arg;
}

Now VSCode does show the documentation:现在 VSCode 确实显示了文档:

在此处输入图片说明

It feels like a trick, but I fail to see any disadvantages to it?感觉就像一个把戏,但我看不出它有什么缺点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript:使用特定的扩展接口来满足通用“扩展”类型参数? - Typescript: use specific extended interface to satisfy generic “extends” type argument? 如何在 Typescript Function 接口中约束通用类型? - How do you constrain a Generic type in a Typescript Function Interface? 如何使用具有通用类型的 Typescript Function 接口? - How do you use a Typescript Function Interface with a Generic type? 在 typescript 中扩展泛型 T 的接口 - Extends interface of generic type T in typescript 通用接口内的 Typescript 函数来推断参数类型 - Typescript function inside generic interface to infer argument type Typescript 通用接口 function 参数类型推断错误 - Typescript generic interface function argument type inference error 如何在 TypeScript 中使用通用和默认参数键入 function - How to type function with generic and default argument in TypeScript 如何创建一个泛型 typescript 类型 function 并从参数类型推断出返回类型? - How do I make a generic typescript type function that has the return type inferred from the argument type? 如何键入检查参数是否扩展类型参数并返回 typescript 中的参数的 function? - How to type a function that checks if argument extends type argument and returns the argument in typescript? function 的 Typescript 参数作为接口或类型 - Typescript argument of function as interface or type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM