Javascript 对象的JSDoc,其键是类的实例

Javascript 对象的JSDoc,其键是类的实例,javascript,jsdoc,Javascript,Jsdoc,标题可能有点不清楚,但这里有一个代码: fruits.js class Fruit { constructor(color, taste) { this.color = color; this.taste = taste; } get fruitColor() { return this.color; } get fruitTaste() { return this.taste; } } const Fruits = { APPL

标题可能有点不清楚,但这里有一个代码:

fruits.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 };
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();
my.class.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 };
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(就问题而言实际上并不重要)

所以我有了这个对象
水果
。它的每个键值都是类Fruit的一个实例(但具有不同的属性)。当我将这个对象的属性作为参数传递时,我想记录它,这样我就可以在一个函数中获取intellisense,该函数可以访问'Fruit'类方法

当然,我不想硬编码它的属性,因为对象
水果
可以有很多不同的键(橙色、菠萝、梨等)

有没有这样的说法:

/**
* @param {propertyvlaue of type Fruits}
*/
function Fruits() {
  return {
      APPLE: new Fruit("green", "sweet"),
      LEMON: new Fruit("yellow", "sour"),
  }
};
var fruit = Fruits();
fruit.APPLE //Correct
fruit.dummy //Error


但问题是,
MyClass
对class
Fruit
一无所知(这是
Fruits
对象中每个键的值)

指示对象键类型的方法是使用
object
。例如:

/** @typedef {(string|boolean)} */
const Sometype = null;

/** @type !Object<!Sometype, string> */
const objectWithCustomKeyTypes = {}

objectWithCustomKeyTypes['foo'] = 'is a string';
objectWithCustomKeyTypes[25] = 'is a number';
虽然我使用了一个简单的类型定义来证明它可以用于自定义类型,但它也可以用于内置类型和第三方类型


Ref:

指示对象键类型的方法是使用
Object
。例如:

/** @typedef {(string|boolean)} */
const Sometype = null;

/** @type !Object<!Sometype, string> */
const objectWithCustomKeyTypes = {}

objectWithCustomKeyTypes['foo'] = 'is a string';
objectWithCustomKeyTypes[25] = 'is a number';
虽然我使用了一个简单的类型定义来证明它可以用于自定义类型,但它也可以用于内置类型和第三方类型


Ref:

几周前我也遇到了同样的问题,typescript有一个有趣的功能,请确保检查一次

您可以将其更改为:

/**
* @param {propertyvlaue of type Fruits}
*/
function Fruits() {
  return {
      APPLE: new Fruit("green", "sweet"),
      LEMON: new Fruit("yellow", "sour"),
  }
};
var fruit = Fruits();
fruit.APPLE //Correct
fruit.dummy //Error
当你写作时:

type T4 = ReturnType<typeof Fruits>;
//will infer to on tooltip
//type T4 = {
//    APPLE: Fruit;
//    LEMON: Fruit;
//}
T4型=返回型;
//将在工具提示上推断为
//T4型={
//苹果:水果;
//柠檬:水果;
//}

更新了

几周前我也遇到了同样的问题,typescript有一个有趣的功能,请务必检查一下

您可以将其更改为:

/**
* @param {propertyvlaue of type Fruits}
*/
function Fruits() {
  return {
      APPLE: new Fruit("green", "sweet"),
      LEMON: new Fruit("yellow", "sour"),
  }
};
var fruit = Fruits();
fruit.APPLE //Correct
fruit.dummy //Error
当你写作时:

type T4 = ReturnType<typeof Fruits>;
//will infer to on tooltip
//type T4 = {
//    APPLE: Fruit;
//    LEMON: Fruit;
//}
T4型=返回型;
//将在工具提示上推断为
//T4型={
//苹果:水果;
//柠檬:水果;
//}
更新

我尝试使用
对象
,但我想我遗漏了什么。我如何才能让
MyClass
知道什么是
Fruit
类(仅在fruits.js中使用)?我尝试使用
Object
但我想我遗漏了一些东西。如何让
MyClass
知道什么是
Fruit
类(仅在fruits.js中使用)?