[英]JSDoc for object with keys that are instances of the class
The title might be a bit unclear, but here's a code:标题可能有点不清楚,但这里有一个代码:
fruits.js水果.js
class Fruit {
constructor(color, taste) {
this.color = color;
this.taste = taste;
}
get fruitColor() {
return this.color;
}
get fruitTaste() {
return this.taste;
}
}
const Fruits = {
APPLE: new Fruit("green", "sweet"),
LEMON: new Fruit("yellow", "sour"),
};
module.exports = { Fruits };
my.class.js我的.class.js
const { Fruits } = require("./fruits");
class MyClass {
// I want a jsdoc here in order to get intellisense
// and be able to have access to all fruit methods like 'fruitColor'
getFruitColor(fruit) {
return fruit.fruitColor;
}
getAppleColor() {
return this.getFruitColor(Fruits.APPLE);
}
}
module.exports = new MyClass();
index.js (does not actually matters in terms of the question) index.js (就问题而言实际上并不重要)
const MyClass = require("./my.class");
const appleColor = MyClass.getAppleColor();
console.log(appleColor);
So I have this object Fruits
.所以我有这个对象Fruits
。 Each key value of it is an instance of the class Fruit (but with a different properties).它的每个键值都是 Fruit 类的一个实例(但具有不同的属性)。 When I'm passing this object's property as an argument I want to document it so I can get intellisense inside a function which accessing the 'Fruit' class methods.当我将此对象的属性作为参数传递时,我想记录它,以便我可以在访问“Fruit”类方法的函数中获得智能感知。
Of course I don't want to hardcode it's properties because object Fruits
can have lot's of different keys (orange, pineapple, pear etc. ).当然,我不想硬编码它的属性,因为对象Fruits
可以有很多不同的键(橙色、菠萝、梨等)。
Is ther some way to say something like:有什么办法可以这样说:
/**
* @param {propertyvlaue of type Fruits}
*/
or或者
/**
* @param {fruit: <typeof Fruits.>}
*/
Or should I maybe use @typedef somewhere in MyClass
?或者我应该在MyClass
某个地方使用 @typedef 吗?
UPD: After trying to understand Graham P Heath answer I tried: UPD:在尝试了解 Graham P Heath 的答案后,我尝试了:
const { Fruits } = require("./fruits");
class MyClass {
/**@param {Object.<string, Fruit>} */
getFruitColor(fruit) {
return fruit.fruitColor;
}
But the problem is that MyClass
does not know anything about class Fruit
(which is the value of each key inside Fruits
object)但问题是MyClass
对Fruit
类一无所知(即Fruits
对象中每个键的值)
The way to indicate a type of an Object's keys is to use Object<keyType, valueType>
.指示对象键类型的方法是使用Object<keyType, valueType>
。 For example:例如:
/** @typedef {(string|boolean)} */
const Sometype = null;
/** @type !Object<!Sometype, string> */
const objectWithCustomKeyTypes = {}
objectWithCustomKeyTypes['foo'] = 'is a string';
objectWithCustomKeyTypes[25] = 'is a number';
In this example, the last line throws a warning:在这个例子中,最后一行抛出一个警告:
input0:8:25: WARNING - [JSC_TYPE_MISMATCH] restricted index type
found : number
required: (boolean|string)
8| objectWithCustomKeyTypes[25] = 'is a number';
^^
While I've used a simple type def to demonstrate that this works with custom types, it should work with built in and third party types as well.虽然我使用了一个简单的类型 def 来证明这适用于自定义类型,但它也应该适用于内置类型和第三方类型。
I also came across the same problem few weeks ago and typescript has an interesting feature ReturnType , make sure to check it once!几周前我也遇到了同样的问题,打字稿有一个有趣的功能ReturnType ,一定要检查一次!
and you can change it to something like:您可以将其更改为:
function Fruits() {
return {
APPLE: new Fruit("green", "sweet"),
LEMON: new Fruit("yellow", "sour"),
}
};
var fruit = Fruits();
fruit.APPLE //Correct
fruit.dummy //Error
and when you write:当你写:
type T4 = ReturnType<typeof Fruits>;
//will infer to on tooltip
//type T4 = {
// APPLE: Fruit;
// LEMON: Fruit;
//}
UPDATED playground更新的游乐场
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.