TypeScript/tests/cases/conformance/salsa/propertiesOfGenericConstructorFunctions.ts

57 lines
1 KiB
TypeScript
Raw Normal View History

Constructor functions as classes (#32944) * Initial implementation The original test passes but I haven't run any other tests yet, so I assume the world is now broken. * Append constructor function construct sigs Instead of overwriting them * Grab bag of improvements. 1. Mark @class-tagged functions with Class too. 2. Only gather local type parameters of constructor functions. 3. Remove getJSClassType calls with getDeclaredTypeOfSymbol. 4. Add a couple more failing tests. getDeclaredTypeOfClassOrInterface now needs to understand prototype assignment. That's next, I think. * Prototype assignments work now 1. Binder marks prototype assignments as Class now. 2. Checker merges prototype assignments using the same merge code as for functions and their declarations. No more intersections. Many fewer failing tests now. * Mark prototype-property assignments as Class Even if there are no this-property assignments in them. (Then why are you using a class?). * Simplify getJSClassType, remove calls to its guts It's probably not needed because now it's just a conditional call to getDeclaredTypeOfSymbol, and I think most callers already know whether they have a JS constructor function beforehand. * isJSDocConstructor doesn't need to check prototype anymore Because all the properties are merged during getDeclaredTypeOfSymbol. * outer type parameter lookup follow prototype assignment * this-type and -expression support in ctor funcs Pretty cool! * Fix remaining tests * Fix minor lint * Delete now-unused code * Add class flag to nested class declarations Also remove old TODOs
2019-08-19 23:12:53 +02:00
// @Filename: propertiesOfGenericConstructorFunctions.js
// @allowJs: true
// @checkJs: true
// @noEmit: true
/**
* @template {string} K
* @template V
* @param {string} ik
* @param {V} iv
*/
function Multimap(ik, iv) {
/** @type {{ [s: string]: V }} */
this._map = {};
// without type annotation
this._map2 = { [ik]: iv };
};
/** @type {Multimap<"a" | "b", number>} with type annotation */
const map = new Multimap("a", 1);
// without type annotation
const map2 = new Multimap("m", 2);
/** @type {number} */
var n = map._map['hi']
/** @type {number} */
var n = map._map2['hi']
/** @type {number} */
var n = map2._map['hi']
/** @type {number} */
var n = map._map2['hi']
/**
* @class
* @template T
* @param {T} t
*/
function Cp(t) {
this.x = 1
this.y = t
}
Cp.prototype = {
m1() { return this.x },
m2() { this.z = this.x + 1; return this.y }
}
var cp = new Cp(1)
/** @type {number} */
var n = cp.x
/** @type {number} */
var n = cp.y
/** @type {number} */
var n = cp.m1()
/** @type {number} */
var n = cp.m2()