如何在 JSDoc 中类似 TypeScript 那样输入“keyof instance”?

huangapple go评论52阅读模式
英文:

How to type keyof instace in JSDOC similarily to TS?

问题

如何在JavaScript(JSDOC)中像TypeScript一样键入实例的关键字?

TS很好:

const a1 = new Car('Audi A1', 4, Power.solar);
const a380 = new Plane('Airbus A380', 800, 4);

function showProperty<T>(thing: T, prop: keyof T) {
    console.log(`${String(prop)}:`, thing[prop]);
}

showProperty(a1, 'power');
showProperty(a380, 'engines');

完整的TS代码链接: 点击此处

但我不知道如何在jsdoc中实现:

const a1 = new Car('Audi A1', 4, 'petrol');
const a380 = new Plane('Airbus A380', 800, 4);

/**
 * @param {Thing} thing
 * @param {keyof Thing} prop
 */
function showProperty(thing, prop) {
    console.log(`${String(prop)}:`, thing[prop]);
}

showProperty(a1, 'capacity'); // 我想在这里看到 "power" 属性...!
showProperty(a380, 'capacity'); // 我想在这里看到 "engines" 属性...!

完整的JS代码链接: [点击此处](https://www.typescriptlang.org/play?filetype=js#code/MYGwhgzhAEAqAWBLAdgc2gbwFDV9A9AFSE57QlnnQACADmAE5gC2mEALgyqgL7TIsApqTyEa9JqwzIArswBGghn2Bh6wROwCeI3IXy7gAe2QcGM4OyMMAFAOaCANNFXrNWgJSZdZdkggAdADE9oLQALz8QgDcPnh+iIFBrmAa2hEuaqnusWQ8ukQUZGLU2rRhGGbc+ZT6uiFCGTLIACaCAGYogi25eIU+JWUVsgpKNcUGZMlZaVpNrR1dPbqoguxRDjZe2JR4DGsyDMjQCUmhvbjjuKvrKbNb3ru4++yHx6fBdzm6+flYxqZ1gAFIwAdyUGR2eHKnCMIAAXNAAOQwhhwpGOXQtRCCCCCBHI7G4-EYlaQRFI1CQUlkCBwxgUungBiknixf7gKDQADCjGgggAHuxBK0YAhuI8+sQBuJGCw2JxqhthLVZZJMCNFMpMm5tDK6HKpABrQRaIztE5acrm6Ag8Ha2hgpQDSZ4AFmCxWWyhZxfbTOR327ZxXAQGTlb1CX0zdweC6UD5BQMQyLJhjxq7QJNO9MrNbQNMPKG7F5vE7+YJpjNYP6gSAwIHgZBhQXC0VwJBoSW4fq1fUSeWVRVoPihfuGjVyLUqGN61UG9XSKdjfloLoQF2GEweyzWOxRnXZf2r1Dr4NPaBhiP7hzR3WeeO+CtBEWn5swSKv9fVqZf9-xm4T3XIsQ2gUsjnLRJgj-XFq1rbd1jAABGDJm1BHlGBsJEAEEZGxaBsKQjFoAAFmcFE1jREAkTjf4EOgMAAGYAA4AAZUMEdDGzAZssOwxAGHkGQYGwljWOItjWOcEjaKwXsSgHKRxRHSC0BwBSJwwE0zQtZTeALNFaHUgx2maSxEBMS94DBIFDKUbQbASNAA0M883W3OFBACEAjFQGwAAMABIMAAZWHPzaFcnh4X85wnNQABtSKjFoABdWi-ggazQVslL7K0Gx

英文:

How can I type keyof instance in Javascript (JSDOC) similarily to Typescript?

TS is just fine:

const a1 = new Car(&#39;Audi A1&#39;, 4, Power.solar);
const a380 = new Plane(&#39;Airbus A380&#39;, 800, 4);

function showProperty&lt;T&gt;(thing: T, prop: keyof T) {
    console.log(`${String(prop)}:`, thing[prop]);
}

showProperty(a1, &#39;power&#39;);
showProperty(a380, &#39;engines&#39;);

link to full TS code

but I don't know how to do it with jsdoc:

const a1 = new Car(&#39;Audi A1&#39;, 4, &#39;petrol&#39;);
const a380 = new Plane(&#39;Airbus A380&#39;, 800, 4);

/**
 * @param {Thing} thing
 * @param {keyof Thing} prop
 */
function showProperty(thing, prop) {
    console.log(`${String(prop)}:`, thing[prop]);
}

showProperty(a1, &#39;capacity&#39;); // I want to see the &quot;power&quot; property here...!
showProperty(a380, &#39;capacity&#39;); // I want to see the &quot;engines&quot; property here...!

link to full JS code

答案1

得分: 2

使用TypeScript中的JSDoc注释,您可以使用@template标记为函数声明一个类型参数。这类似于TypeScript使用尖括号(<T>)定义类型参数的语法。

/**
 * @template T
 * @param {T} thing
 * @param {keyof T} prop
 */
function showProperty(thing, prop) {
    console.log(`${String(prop)}:`, thing[prop]);
}

VSCode和其他代码编辑器现在应该能够看到所有属性

链接到修改后的JS代码

英文:

With JSDoc annotations in TypeScript, you can use the @template tag to declare a type parameter for the function. This is similar to the TypeScript syntax of using angle brackets (&lt;T&gt;) to define a type parameter.

/**
 * @template T
 * @param {T} thing
 * @param {keyof T} prop
 */
function showProperty(thing, prop) {
    console.log(`${String(prop)}:`, thing[prop]);
}

VSCode and other code editors should now be able to see all the properties

Link to modified JS code

huangapple
  • 本文由 发表于 2023年2月19日 22:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定