mikewesthad / phaser-matter-collision-plugin

A plugin for making it easier to manage collisions with Phaser 3 + Matter.js
MIT License
99 stars 9 forks source link

Import in TypeScript #3

Closed vogloblinsky closed 5 years ago

vogloblinsky commented 5 years ago

After installed this plugin in a Phaser 3 TypeScript project, VSCode and TypeScript doesn't find it.

mikewesthad commented 5 years ago

Hi @vogloblinsky, thanks for opening up an issue. Since I don't work in TS, could you clarify a bit for me? Do you just mean that IntelliSense is missing information as you type? There are no types published for the plugin, so that's to be expected.

Or do you mean that you the TS build process is throwing an error? In that case, does doing something like this fix it (source):

declare var PhaserMatterCollisionPlugin: any;
vogloblinsky commented 5 years ago

Just done that:

npm install --save phaser-matter-collision-plugin

and in my TypeScript main file :

import PhaserMatterCollisionPlugin from 'phaser-matter-collision-plugin';

I think it is related to my tsconfig.json file :

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es6"
  },
  "include": [
    "./src/**/*"
  ]
}
mikewesthad commented 5 years ago

I did a quick check with a demo TS project, and it should work fine as far as I can tell.

With your snippets, it seems you either need to add "allowSyntheticDefaultImports": true to your tsconfig or use import * as PhaserMatterCollisionPlugin from "phaser-matter-collision-plugin";. I'm going to assume your original error was that PhaserMatterCollisionPlugin was undefined after importing? This should fix it.

mikewesthad commented 5 years ago

I also suspect that

    "noImplicitAny": true,
    "module": "es6",
    "target": "es6"

may be problematic for importing a non-ts library into a ts project (my demo project didn't have them).

mikewesthad commented 5 years ago

I'm going to close this issue. I've had some time to look into TypeScript recently and there's nothing in this library that would prevent you from using it in a TS project. The library doesn't have types and wasn't written in TS, but you can still use it. Here's a working CodeSandbox with Phaser + TypeScript + PhaserMatterCollisionPlugin.

It is indeed your tsconfig that is giving you headaches.

If you are curious, you can read more about the TS support for untyped JS libraries in this issue. It's annoying that this use case isn't better handled currently, but at least it works.

Anyway, hope that helps anyone who stumbles upon this issue.

TheLucifurry commented 3 years ago

Hi, maybe these type declarations can be useful to someone)

// global.d.ts
type TMCEventData = {
  bodyA: MatterJS.Body,
  bodyB: MatterJS.Body,
  gameObjectA: Phaser.GameObjects.GameObject,
  gameObjectB: Phaser.GameObjects.GameObject,
  pair: MatterJS.Pair,
}
type TMCConfig = {
  objectA: MatterJS.Body | MatterJS.Body[]
  objectB?: MatterJS.Body | MatterJS.Body[]
  callback: (event: TMCEventData) => void
  context?: any
}
type TMCUnsubscribeCallback = () => void;
declare namespace Phaser {
  interface Scene {
    matterCollision: {
      addOnCollideStart: (config: TMCConfig) => TMCUnsubscribeCallback
      addOnCollideActive: (config: TMCConfig) => TMCUnsubscribeCallback
      addOnCollideEnd: (config: TMCConfig) => TMCUnsubscribeCallback
    }
  }
}

(Errors possible)

mikewesthad commented 3 years ago

@Wexelus Thanks for contributing these!

It's been a while since this issue was opened, and I now regularly work in TS. I've got a WIP local branch converting this library to TS, so I'll get that pushed soon and keep this thread updated.

mikewesthad commented 3 years ago

Bit of a delay, but the typescript version is live as of v1.0.0

MEJSIK commented 3 years ago

Bit of a delay, but the typescript version is live as of v1.0.0

Yeah, but I have problem with importing the plugin. I got this error: image

I'm using Parcel and Typescript

mikewesthad commented 3 years ago

Thanks for reporting this! I just opened a new issue for that #8 and will investigate (hopefully this evening)