Hi all,
I’m trying to transition into using Babylon with TypeScript on a project. I’ve previously used it with JavaScript, but I’d like the stronger typing.
I took a look at this tutorial page:
https://doc.babylonjs.com/setup/frameworkPackages/es6Support
… and it looked almost like what I want, but I have no interest in using webpack etc. I just want a simple starting point for a plain old TypeScript project that uses Babylon.
Nonetheless, I thought that tutorial might make a good starting point! So I created a new directory, in which:
$ npm install -D typescript
$ npm install @babylonjs/core @babylonjs/materials
$ mkdir src
I then copied the example HTML and tsconfig.json
files from the tutorial page into the directory, and copied the example TypeScript file from the tutorial page into src
.
Let’s see what happens then we run tsc
:
$ tsc
node_modules/@babylonjs/core/Engines/engine.d.ts:1216:29 - error TS2552: Cannot find name 'ReadonlySet'. Did you mean 'Readonly'?
1216 type GPUSupportedFeatures = ReadonlySet<string>;
~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:1218:29 - error TS2552: Cannot find name 'ReadonlySet'. Did you mean 'Readonly'?
1218 type WGSLLanguageFeatures = ReadonlySet<string>;
~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2497:5 - error TS1169: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
2497 [Symbol.iterator](): IterableIterator<XRInputSource>;
~~~~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2497:6 - error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.
2497 [Symbol.iterator](): IterableIterator<XRInputSource>;
~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2497:26 - error TS2304: Cannot find name 'IterableIterator'.
2497 [Symbol.iterator](): IterableIterator<XRInputSource>;
~~~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2502:16 - error TS2304: Cannot find name 'IterableIterator'.
2502 entries(): IterableIterator<[number, XRInputSource]>;
~~~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2503:13 - error TS2304: Cannot find name 'IterableIterator'.
2503 keys(): IterableIterator<number>;
~~~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2504:15 - error TS2304: Cannot find name 'IterableIterator'.
2504 values(): IterableIterator<XRInputSource>;
~~~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2766:20 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
2766 type XRAnchorSet = Set<XRAnchor>;
~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2851:19 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
2851 type XRPlaneSet = Set<XRPlane>;
~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2917:26 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
2917 interface XRHand extends Map<XRHandJoint, XRJointSpace> {
~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:2950:31 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
2950 abstract class XRHand extends Map<XRHandJoint, XRJointSpace> implements XRHand {}
~~~
node_modules/@babylonjs/core/Engines/engine.d.ts:3467:18 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
3467 type XRMeshSet = Set<XRMesh>;
~~~
node_modules/@babylonjs/core/Lights/light.d.ts:240:33 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
240 _shadowGenerators: Nullable<Map<Nullable<Camera>, IShadowGenerator>>;
~~~
node_modules/@babylonjs/core/Lights/light.d.ts:325:37 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
325 getShadowGenerators(): Nullable<Map<Nullable<Camera>, IShadowGenerator>>;
~~~
node_modules/@babylonjs/core/Misc/coroutine.d.ts:6:38 - error TS2583: Cannot find name 'Iterator'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
6 type CoroutineBase<TStep, TReturn> = Iterator<TStep, TReturn, void> & IterableIterator<TStep>;
~~~~~~~~
node_modules/@babylonjs/core/Misc/coroutine.d.ts:6:71 - error TS2304: Cannot find name 'IterableIterator'.
6 type CoroutineBase<TStep, TReturn> = Iterator<TStep, TReturn, void> & IterableIterator<TStep>;
~~~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Misc/coroutine.d.ts:12:32 - error TS2304: Cannot find name 'IteratorResult'.
12 export type CoroutineStep<T> = IteratorResult<void, T>;
~~~~~~~~~~~~~~
node_modules/@babylonjs/core/Misc/PerformanceViewer/performanceViewerCollector.d.ts:47:45 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
47 readonly metadataObservable: Observable<Map<string, IPerfMetadata>>;
~~~
Found 19 errors in 4 files.
Errors Files
13 node_modules/@babylonjs/core/Engines/engine.d.ts:1216
2 node_modules/@babylonjs/core/Lights/light.d.ts:240
3 node_modules/@babylonjs/core/Misc/coroutine.d.ts:6
1 node_modules/@babylonjs/core/Misc/PerformanceViewer/performanceViewerCollector.d.ts:47
Wow, that’s … not good. 19 errors? Looks like it may be a problem with the TypeScript compiler setup? I wonder what happens if I delete the tsconfig.json
file and run tsc
again after generating a “clean” tsconfig.json
?
$ rm tsconfig.json
$ tsc --init
Created a new tsconfig.json with:
TS
target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true
You can learn more at https://aka.ms/tsconfig
$ tsc
$
Great, compiled with no errors! After changing the script path in the HTML file from the example page, I load the webpage and get … nothing. The JavaScript log says:
ReferenceError: Can't find variable: exports
Hmm. Maybe if I change the script type in the HTML file to “module”? Nope, that didn’t work.
Can anyone tell me how to get this working? I have no interest in additional junk like webpack, I’d just like to know the correct TypeScript compiler settings / HTML file additions to make this simple example work as expected.
Thanks!