Cannot use p5.js in typeScript and Webpack

北城余情 提交于 2019-12-06 06:21:29

I take a look at it and it turns out 'p5' typings is not correct. So you can't use it. Check out some of its issues here: https://github.com/processing/p5.js/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+typescript

And for you, your config is messing you up in some way. You can easily see the actual error by creating an index.ts:

import 'p5'
console.log(p5)

and tries to transpile it with tsc:

node_modules/p5/lib/p5.d.ts(555,19): error TS2304: Cannot find name 'COLOR_MODE'.
node_modules/p5/lib/p5.d.ts(871,87): error TS2304: Cannot find name 'ARC_MODE'.

... removing some errors for brevity

node_modules/p5/lib/p5.d.ts(10312,5): error TS2416: Property 'amp' in type 'Noise' is not assignable to the same property in base type 'Oscillator'.
  Type '(volume: number | object, rampTime?: number, timeFromNow?: number) => void' is not assignable to type '(vol: number | object, rampTime?: number, timeFromNow?: number) => AudioParam'.
    Type 'void' is not assignable to type 'AudioParam'.

Since p5 is used globally and I can't find an example to use is as a module, the following will work fine:

// index.ts
declare const p5: any
// code away

p5.js has an official typings package. Install it with npm i @types/p5 --save-dev will give you real typing instead of the any type in the work around posted by unional.

Here is the typings package on npm: https://www.npmjs.com/package/@types/p5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!