phaser3-docs icon indicating copy to clipboard operation
phaser3-docs copied to clipboard

Property 'Matter' does not exist on type 'typeof Matter'

Open vacarsu opened this issue 7 years ago • 4 comments

Having difficulty accessing Phaser.Physics.Matter.Matter

compiler throws - Property 'Matter' does not exist on type 'typeof Matter'

I am trying to recreate this example - http://labs.phaser.io/edit.html?src=src\game%20objects\tilemap\collision\matter%20platformer%20with%20wall%20jumping.js

vacarsu avatar May 10 '18 00:05 vacarsu

Are you using TypeScript? I get the same compilation error, but Phaser.Physics.Matter.Matter does indeed exist (my code runs fine); it just doesn't show up in the TypeScript defs I guess. @photonstorm would it be possible to pull @types/matter.js into this repository, or something similar? It would be nice to have autocomplete when using Matter.js in Phaser.

samestep avatar Nov 09 '18 23:11 samestep

I figured out a nice hack for now: add @types/matter-js to your package.json, then put this at the top of your source file:

import * as MatterJS from 'matter-js';
// @ts-ignore: Property 'Matter' does not exist on type 'typeof Matter'.
const Matter: typeof MatterJS = Phaser.Physics.Matter.Matter;

samestep avatar Nov 09 '18 23:11 samestep

You may also write it without having // @ts-ignore

const Matter = (Phaser.Physics.Matter as Record<string, unknown>).Matter as typeof MatterJS;

AMerkuri avatar Jun 15 '21 21:06 AMerkuri

No need for any of that, this is how it's done in Typescript (*reference):

// old JS way: const { Bodies } = Phaser.Physics.Matter.Matter;
const Bodies = scene.matter.bodies;
// or: const Bodies = new Phaser.Physics.Matter.MatterPhysics(scene).bodies;

RS-DU34 avatar Jul 28 '22 21:07 RS-DU34