Skip to content

Commit 85f9b3c

Browse files
authored
Always widen resulting type in getWidenedTypeForAssignmentDeclaration (#3648)
1 parent 245ae95 commit 85f9b3c

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

internal/checker/checker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17708,9 +17708,10 @@ func (c *Checker) getWidenedTypeForAssignmentDeclaration(symbol *ast.Symbol) *Ty
1770817708
}
1770917709
}
1771017710
if t == nil {
17711-
t = c.getWidenedType(c.getUnionType(types))
17711+
t = c.getUnionType(types)
1771217712
}
1771317713
}
17714+
t = c.getWidenedType(t)
1771417715
// report an all-nullable or empty union as an implicit any in JS files
1771517716
if symbol.ValueDeclaration != nil && ast.IsInJSFile(symbol.ValueDeclaration) &&
1771617717
c.filterType(t, func(c *Type) bool { return c.Flags() & ^TypeFlagsNullable != 0 }) == c.neverType {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/widenedThisPropertyAssignment.ts] ////
2+
3+
=== main.js ===
4+
export class CC {
5+
>CC : Symbol(CC, Decl(main.js, 0, 0))
6+
7+
constructor() {
8+
this.stuffs = {}
9+
>this.stuffs : Symbol(CC.stuffs, Decl(main.js, 1, 19), Decl(main.js, 7, 23))
10+
>this : Symbol(CC, Decl(main.js, 0, 0))
11+
>stuffs : Symbol(CC.stuffs, Decl(main.js, 1, 19), Decl(main.js, 7, 23))
12+
}
13+
/**
14+
* @param {Object} stuffs
15+
*/
16+
addStuffs(stuffs) {
17+
>addStuffs : Symbol(CC.addStuffs, Decl(main.js, 3, 5))
18+
>stuffs : Symbol(stuffs, Decl(main.js, 7, 14))
19+
20+
this.stuffs = { ...stuffs, ...this.stuffs }
21+
>this.stuffs : Symbol(CC.stuffs, Decl(main.js, 1, 19), Decl(main.js, 7, 23))
22+
>this : Symbol(CC, Decl(main.js, 0, 0))
23+
>stuffs : Symbol(CC.stuffs, Decl(main.js, 1, 19), Decl(main.js, 7, 23))
24+
>stuffs : Symbol(stuffs, Decl(main.js, 7, 14))
25+
>this.stuffs : Symbol(CC.stuffs, Decl(main.js, 1, 19), Decl(main.js, 7, 23))
26+
>this : Symbol(CC, Decl(main.js, 0, 0))
27+
>stuffs : Symbol(CC.stuffs, Decl(main.js, 1, 19), Decl(main.js, 7, 23))
28+
}
29+
}
30+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/widenedThisPropertyAssignment.ts] ////
2+
3+
=== main.js ===
4+
export class CC {
5+
>CC : CC
6+
7+
constructor() {
8+
this.stuffs = {}
9+
>this.stuffs = {} : {}
10+
>this.stuffs : any
11+
>this : this
12+
>stuffs : any
13+
>{} : {}
14+
}
15+
/**
16+
* @param {Object} stuffs
17+
*/
18+
addStuffs(stuffs) {
19+
>addStuffs : (stuffs: Object) => void
20+
>stuffs : Object
21+
22+
this.stuffs = { ...stuffs, ...this.stuffs }
23+
>this.stuffs = { ...stuffs, ...this.stuffs } : { constructor: Function; toString(): string; toLocaleString(): string; valueOf(): Object; hasOwnProperty(v: PropertyKey): boolean; isPrototypeOf(v: Object): boolean; propertyIsEnumerable(v: PropertyKey): boolean; }
24+
>this.stuffs : {}
25+
>this : this
26+
>stuffs : {}
27+
>{ ...stuffs, ...this.stuffs } : { constructor: Function; toString(): string; toLocaleString(): string; valueOf(): Object; hasOwnProperty(v: PropertyKey): boolean; isPrototypeOf(v: Object): boolean; propertyIsEnumerable(v: PropertyKey): boolean; }
28+
>stuffs : Object
29+
>this.stuffs : {}
30+
>this : this
31+
>stuffs : {}
32+
}
33+
}
34+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @checkJs: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/typescript-go/issues/3642
5+
6+
// @filename: main.js
7+
export class CC {
8+
constructor() {
9+
this.stuffs = {}
10+
}
11+
/**
12+
* @param {Object} stuffs
13+
*/
14+
addStuffs(stuffs) {
15+
this.stuffs = { ...stuffs, ...this.stuffs }
16+
}
17+
}

0 commit comments

Comments
 (0)