* Classes can extend JS constructor functions Now ES6 classes can extend ES5 constructor functions, although only those written in a JS file. Note that the static side assignability is checked. I need to write tests to make sure that instance side assignability is checked too. I haven't tested generic constructor functions yet either. * Test static+instance assignability errors+generics Note that generics do not work. * Cleanup from PR comments * Even more cleanup * Update case of function name
94 lines
1.8 KiB
TypeScript
94 lines
1.8 KiB
TypeScript
// @noEmit: true
|
|
// @allowJs: true
|
|
// @checkJs: true
|
|
// @Filename: first.js
|
|
|
|
/**
|
|
* @constructor
|
|
* @param {number} numberOxen
|
|
*/
|
|
function Wagon(numberOxen) {
|
|
this.numberOxen = numberOxen
|
|
}
|
|
/** @param {Wagon[]=} wagons */
|
|
Wagon.circle = function (wagons) {
|
|
return wagons ? wagons.length : 3.14;
|
|
}
|
|
/** @param {*[]=} supplies - *[]= is my favourite type */
|
|
Wagon.prototype.load = function (supplies) {
|
|
}
|
|
// ok
|
|
class Sql extends Wagon {
|
|
constructor() {
|
|
super(); // error: not enough arguments
|
|
this.foonly = 12
|
|
}
|
|
/**
|
|
* @param {Array.<string>} files
|
|
* @param {"csv" | "json" | "xmlolololol"} format
|
|
* This is not assignable, so should have a type error
|
|
*/
|
|
load(files, format) {
|
|
if (format === "xmlolololol") {
|
|
throw new Error("please do not use XML. It was a joke.");
|
|
}
|
|
}
|
|
}
|
|
var db = new Sql();
|
|
db.numberOxen = db.foonly
|
|
|
|
// error, can't extend a TS constructor function
|
|
class Drakkhen extends Dragon {
|
|
|
|
}
|
|
|
|
// @Filename: second.ts
|
|
|
|
/**
|
|
* @constructor
|
|
*/
|
|
function Dragon(numberEaten: number) {
|
|
this.numberEaten = numberEaten
|
|
}
|
|
// error!
|
|
class Firedrake extends Dragon {
|
|
constructor() {
|
|
super();
|
|
}
|
|
}
|
|
// ok
|
|
class Conestoga extends Wagon {
|
|
constructor(public drunkOO: true) {
|
|
// error: wrong type
|
|
super('nope');
|
|
}
|
|
// should error since others is not optional
|
|
static circle(others: (typeof Wagon)[]) {
|
|
return others.length
|
|
}
|
|
}
|
|
var c = new Conestoga(true);
|
|
c.drunkOO
|
|
c.numberOxen
|
|
|
|
// @Filename: generic.js
|
|
|
|
/**
|
|
* @template T
|
|
* @param {T} flavour
|
|
*/
|
|
function Soup(flavour) {
|
|
this.flavour = flavour
|
|
}
|
|
/** @extends {Soup<{ claim: "ignorant" | "malicious" }>} */
|
|
class Chowder extends Soup {
|
|
log() {
|
|
return this.flavour
|
|
}
|
|
}
|
|
|
|
var soup = new Soup(1);
|
|
soup.flavour
|
|
var chowder = new Chowder();
|
|
chowder.flavour.claim
|
|
|