Skip to content

Commit 37f2e59

Browse files
authored
Cache & widen assigned js prototype type (#32381)
1 parent 89badcc commit 37f2e59

6 files changed

+237
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22750,7 +22750,7 @@ namespace ts {
2275022750
isVariableDeclaration(decl.parent) && getSymbolOfNode(decl.parent));
2275122751
const prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype" as __String);
2275222752
const init = prototype && prototype.valueDeclaration && getAssignedJSPrototype(prototype.valueDeclaration);
22753-
return init ? checkExpression(init) : undefined;
22753+
return init ? getWidenedType(checkExpressionCached(init)) : undefined;
2275422754
}
2275522755

2275622756
function getAssignedJSPrototype(node: Node) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
tests/cases/compiler/index.js(1,16): error TS7006: Parameter 'obj' implicitly has an 'any' type.
2+
tests/cases/compiler/index.js(6,21): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
3+
tests/cases/compiler/index.js(7,20): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
4+
tests/cases/compiler/index.js(8,22): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
5+
tests/cases/compiler/index.js(9,24): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
6+
tests/cases/compiler/index.js(10,20): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
7+
tests/cases/compiler/index.js(11,21): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
8+
tests/cases/compiler/index.js(13,21): error TS7006: Parameter 'ratio' implicitly has an 'any' type.
9+
tests/cases/compiler/index.js(14,2): error TS7023: 'toJSON' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
10+
tests/cases/compiler/index.js(14,35): error TS2339: Property 'rgb' does not exist on type 'Color & { negate: () => Color & any; lighten: (ratio: any) => Color & any; darken: (ratio: any) => Color & any; saturate: (ratio: any) => Color & any; desaturate: (ratio: any) => Color & any; whiten: (ratio: any) => Color & any; blacken: (ratio: any) => Color & any; greyscale: () => Color & any; clearer: (ratio: any) => Color & any; toJSON: () => any; }'.
11+
12+
13+
==== tests/cases/compiler/index.js (10 errors) ====
14+
function Color(obj) {
15+
~~~
16+
!!! error TS7006: Parameter 'obj' implicitly has an 'any' type.
17+
this.example = true
18+
};
19+
Color.prototype = {
20+
negate: function () {return this;},
21+
lighten: function (ratio) {return this;},
22+
~~~~~
23+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
24+
darken: function (ratio) {return this;},
25+
~~~~~
26+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
27+
saturate: function (ratio) {return this;},
28+
~~~~~
29+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
30+
desaturate: function (ratio) {return this;},
31+
~~~~~
32+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
33+
whiten: function (ratio) {return this;},
34+
~~~~~
35+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
36+
blacken: function (ratio) {return this;},
37+
~~~~~
38+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
39+
greyscale: function () {return this;},
40+
clearer: function (ratio) {return this;},
41+
~~~~~
42+
!!! error TS7006: Parameter 'ratio' implicitly has an 'any' type.
43+
toJSON: function () {return this.rgb();},
44+
~~~~~~
45+
!!! error TS7023: 'toJSON' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
46+
~~~
47+
!!! error TS2339: Property 'rgb' does not exist on type 'Color & { negate: () => Color & any; lighten: (ratio: any) => Color & any; darken: (ratio: any) => Color & any; saturate: (ratio: any) => Color & any; desaturate: (ratio: any) => Color & any; whiten: (ratio: any) => Color & any; blacken: (ratio: any) => Color & any; greyscale: () => Color & any; clearer: (ratio: any) => Color & any; toJSON: () => any; }'.
48+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [index.js]
2+
function Color(obj) {
3+
this.example = true
4+
};
5+
Color.prototype = {
6+
negate: function () {return this;},
7+
lighten: function (ratio) {return this;},
8+
darken: function (ratio) {return this;},
9+
saturate: function (ratio) {return this;},
10+
desaturate: function (ratio) {return this;},
11+
whiten: function (ratio) {return this;},
12+
blacken: function (ratio) {return this;},
13+
greyscale: function () {return this;},
14+
clearer: function (ratio) {return this;},
15+
toJSON: function () {return this.rgb();},
16+
};
17+
18+
//// [index.js]
19+
function Color(obj) {
20+
this.example = true;
21+
}
22+
;
23+
Color.prototype = {
24+
negate: function () { return this; },
25+
lighten: function (ratio) { return this; },
26+
darken: function (ratio) { return this; },
27+
saturate: function (ratio) { return this; },
28+
desaturate: function (ratio) { return this; },
29+
whiten: function (ratio) { return this; },
30+
blacken: function (ratio) { return this; },
31+
greyscale: function () { return this; },
32+
clearer: function (ratio) { return this; },
33+
toJSON: function () { return this.rgb(); }
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/index.js ===
2+
function Color(obj) {
3+
>Color : Symbol(Color, Decl(index.js, 0, 0), Decl(index.js, 2, 2))
4+
>obj : Symbol(obj, Decl(index.js, 0, 15))
5+
6+
this.example = true
7+
>example : Symbol(Color.example, Decl(index.js, 0, 21))
8+
9+
};
10+
Color.prototype = {
11+
>Color.prototype : Symbol(Color.prototype, Decl(index.js, 2, 2))
12+
>Color : Symbol(Color, Decl(index.js, 0, 0), Decl(index.js, 2, 2))
13+
>prototype : Symbol(Color.prototype, Decl(index.js, 2, 2))
14+
15+
negate: function () {return this;},
16+
>negate : Symbol(negate, Decl(index.js, 3, 19))
17+
18+
lighten: function (ratio) {return this;},
19+
>lighten : Symbol(lighten, Decl(index.js, 4, 36))
20+
>ratio : Symbol(ratio, Decl(index.js, 5, 20))
21+
22+
darken: function (ratio) {return this;},
23+
>darken : Symbol(darken, Decl(index.js, 5, 42))
24+
>ratio : Symbol(ratio, Decl(index.js, 6, 19))
25+
26+
saturate: function (ratio) {return this;},
27+
>saturate : Symbol(saturate, Decl(index.js, 6, 41))
28+
>ratio : Symbol(ratio, Decl(index.js, 7, 21))
29+
30+
desaturate: function (ratio) {return this;},
31+
>desaturate : Symbol(desaturate, Decl(index.js, 7, 43))
32+
>ratio : Symbol(ratio, Decl(index.js, 8, 23))
33+
34+
whiten: function (ratio) {return this;},
35+
>whiten : Symbol(whiten, Decl(index.js, 8, 45))
36+
>ratio : Symbol(ratio, Decl(index.js, 9, 19))
37+
38+
blacken: function (ratio) {return this;},
39+
>blacken : Symbol(blacken, Decl(index.js, 9, 41))
40+
>ratio : Symbol(ratio, Decl(index.js, 10, 20))
41+
42+
greyscale: function () {return this;},
43+
>greyscale : Symbol(greyscale, Decl(index.js, 10, 42))
44+
45+
clearer: function (ratio) {return this;},
46+
>clearer : Symbol(clearer, Decl(index.js, 11, 39))
47+
>ratio : Symbol(ratio, Decl(index.js, 12, 20))
48+
49+
toJSON: function () {return this.rgb();},
50+
>toJSON : Symbol(toJSON, Decl(index.js, 12, 42))
51+
52+
};

0 commit comments

Comments
 (0)