Skip to content

Commit 5d54ac8

Browse files
authored
Prefer implicit {} over explicitly declared {} in subtype reduction (#1603)
1 parent 809b2c0 commit 5d54ac8

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

internal/checker/checker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25011,6 +25011,9 @@ func (c *Checker) removeSubtypes(types []*Type, hasObjectTypes bool) []*Type {
2501125011
continue
2501225012
}
2501325013
}
25014+
if (source == c.emptyObjectType || source == c.unknownEmptyObjectType) && target.symbol != nil && c.IsEmptyAnonymousObjectType(target) {
25015+
continue
25016+
}
2501425017
if c.isTypeRelatedTo(source, target, c.strictSubtypeRelation) && (c.getTargetType(source).objectFlags&ObjectFlagsClass == 0 ||
2501525018
c.getTargetType(target).objectFlags&ObjectFlagsClass == 0 ||
2501625019
c.isTypeDerivedFrom(source, target)) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
implicitEmptyObjectType.ts(6,17): error TS2345: Argument of type '{}' is not assignable to parameter of type 'Record<string, string>'.
2+
Index signature for type 'string' is missing in type '{}'.
3+
4+
5+
==== implicitEmptyObjectType.ts (1 errors) ====
6+
// https://github.com/microsoft/typescript-go/issues/1563
7+
8+
function f() {
9+
const v: unknown = "lol";
10+
const acceptsRecord = (record: Record<string, string>) => {};
11+
acceptsRecord(v || {});
12+
~~~~~~~
13+
!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'Record<string, string>'.
14+
!!! error TS2345: Index signature for type 'string' is missing in type '{}'.
15+
}
16+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/implicitEmptyObjectType.ts] ////
2+
3+
=== implicitEmptyObjectType.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/1563
5+
6+
function f() {
7+
>f : Symbol(f, Decl(implicitEmptyObjectType.ts, 0, 0))
8+
9+
const v: unknown = "lol";
10+
>v : Symbol(v, Decl(implicitEmptyObjectType.ts, 3, 7))
11+
12+
const acceptsRecord = (record: Record<string, string>) => {};
13+
>acceptsRecord : Symbol(acceptsRecord, Decl(implicitEmptyObjectType.ts, 4, 7))
14+
>record : Symbol(record, Decl(implicitEmptyObjectType.ts, 4, 25))
15+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
16+
17+
acceptsRecord(v || {});
18+
>acceptsRecord : Symbol(acceptsRecord, Decl(implicitEmptyObjectType.ts, 4, 7))
19+
>v : Symbol(v, Decl(implicitEmptyObjectType.ts, 3, 7))
20+
}
21+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/implicitEmptyObjectType.ts] ////
2+
3+
=== implicitEmptyObjectType.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/1563
5+
6+
function f() {
7+
>f : () => void
8+
9+
const v: unknown = "lol";
10+
>v : unknown
11+
>"lol" : "lol"
12+
13+
const acceptsRecord = (record: Record<string, string>) => {};
14+
>acceptsRecord : (record: Record<string, string>) => void
15+
>(record: Record<string, string>) => {} : (record: Record<string, string>) => void
16+
>record : Record<string, string>
17+
18+
acceptsRecord(v || {});
19+
>acceptsRecord(v || {}) : void
20+
>acceptsRecord : (record: Record<string, string>) => void
21+
>v || {} : {}
22+
>v : unknown
23+
>{} : {}
24+
}
25+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/typescript-go/issues/1563
5+
6+
function f() {
7+
const v: unknown = "lol";
8+
const acceptsRecord = (record: Record<string, string>) => {};
9+
acceptsRecord(v || {});
10+
}

0 commit comments

Comments
 (0)