找不到名称错误 - 如何从 node_modules/@types in angular 导入定义文件?

Cannot find name error - How to import definitions file from node_modules/@types in angular?

所以我有这个 node_modules/@types file 和我需要的定义。但是,我似乎无法将它导入到我的 js 文件中。

我已经完成 npm install @types/p5 并将其添加到我的 tsconfig.json 中,例如:

"types": [
        "node",
        "p5"
     ], 

但仍未找到名称。

我的代码基本上是这样的:

import * as p5 from 'p5';
//imports p5.js

export class Box { 

  ...

  show(p5) {
    p5.rectMode(CENTER);
  {

  ...

}

CENTER 需要定义,但事实并非如此。

CENTER 在你的 p5 实例中,因为你已经从 @types/p5 导入了所有的 p5:

const p = new p5(...); // specify arguments for p5

show(p) {
  p.rectMode(p.CENTER); // p.CENTER returns 'center'
}