TypeScript does not understand the prototypal syntax / constructor functions: there will be no intellisense. It prefers ES6 classes.
The JavaScript Player constructor function is equivalent to this TypeScript class definition:
// -- player.d.ts -- declare class Player { validate(): void; }
Then, to add a method to the Player class, there's a couple of options:
- The more TypeScript idiomatic ways: class inheritance or class composition
- A compromise to keep using the prototypal syntax in TypeScript and still get the intellisense: facade interface
Code examples:
// -- authenticated-player.ts -- import { Player } from './player'; // Option #1a: class inheritance class AuthenticablePlayer extends Player { login() { this.validate(); } } const p1a = new AuthenticablePlayer(); p1a.login(); // Option #1b: class composition + delegation class AuthenticablePlayerB { private player = new Player(); login() { this.validate(); } validate() { this.player.validate(); } } const p1b = new AuthenticablePlayerB(); p1b.login();
--
// Option #2: prototype extension + facade interface + type assertion const Player: any = require('./player'); Player.prototype.login = function() { this.validate(); } interface IPlayer { login(): void; validate(): void; } function createPlayer(): IPlayer { return new Player(); } const p2 = createPlayer(); p2.login();