Skip to content

Empty object not subtype of object with index signature #50013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20621,7 +20621,8 @@ namespace ts {
if (sourceInfo) {
return indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors);
}
if (!(intersectionState & IntersectionState.Source) && isObjectTypeWithInferableIndex(source)) {
if (!(intersectionState & IntersectionState.Source) && isObjectTypeWithInferableIndex(source) &&
!((relation === subtypeRelation || relation === strictSubtypeRelation) && isEmptyAnonymousObjectType(source) && !(getObjectFlags(source) & ObjectFlags.FreshLiteral))) {
// Intersection constituents are never considered to have an inferred index signature
return membersRelatedToIndexInfo(source, targetInfo, reportErrors);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/emptyObjectNarrowing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [emptyObjectNarrowing.ts]
// Repro from #49988

declare function isObject(value: unknown): value is Record<string, unknown>;
const obj = {};
if (isObject(obj)) {
obj['attr'];
}


//// [emptyObjectNarrowing.js]
"use strict";
// Repro from #49988
var obj = {};
if (isObject(obj)) {
obj['attr'];
}


//// [emptyObjectNarrowing.d.ts]
declare function isObject(value: unknown): value is Record<string, unknown>;
declare const obj: {};
20 changes: 20 additions & 0 deletions tests/baselines/reference/emptyObjectNarrowing.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/emptyObjectNarrowing.ts ===
// Repro from #49988

declare function isObject(value: unknown): value is Record<string, unknown>;
>isObject : Symbol(isObject, Decl(emptyObjectNarrowing.ts, 0, 0))
>value : Symbol(value, Decl(emptyObjectNarrowing.ts, 2, 26))
>value : Symbol(value, Decl(emptyObjectNarrowing.ts, 2, 26))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

const obj = {};
>obj : Symbol(obj, Decl(emptyObjectNarrowing.ts, 3, 5))

if (isObject(obj)) {
>isObject : Symbol(isObject, Decl(emptyObjectNarrowing.ts, 0, 0))
>obj : Symbol(obj, Decl(emptyObjectNarrowing.ts, 3, 5))

obj['attr'];
>obj : Symbol(obj, Decl(emptyObjectNarrowing.ts, 3, 5))
}

22 changes: 22 additions & 0 deletions tests/baselines/reference/emptyObjectNarrowing.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/emptyObjectNarrowing.ts ===
// Repro from #49988

declare function isObject(value: unknown): value is Record<string, unknown>;
>isObject : (value: unknown) => value is Record<string, unknown>
>value : unknown

const obj = {};
>obj : {}
>{} : {}

if (isObject(obj)) {
>isObject(obj) : boolean
>isObject : (value: unknown) => value is Record<string, unknown>
>obj : {}

obj['attr'];
>obj['attr'] : unknown
>obj : Record<string, unknown>
>'attr' : "attr"
}

8 changes: 4 additions & 4 deletions tests/baselines/reference/useObjectValuesAndEntries1.types
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ var values2 = Object.values({ a: true, b: 2 }); // (number|boolean)[]
>2 : 2

var entries3 = Object.entries({}); // [string, {}][]
>entries3 : [string, unknown][]
>Object.entries({}) : [string, unknown][]
>entries3 : [string, any][]
>Object.entries({}) : [string, any][]
>Object.entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
>{} : {}

var values3 = Object.values({}); // {}[]
>values3 : unknown[]
>Object.values({}) : unknown[]
>values3 : any[]
>Object.values({}) : any[]
Comment on lines +88 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: is this a desired change?

>Object.values : { <T>(o: { [s: string]: T; } | ArrayLike<T>): T[]; (o: {}): any[]; }
>Object : ObjectConstructor
>values : { <T>(o: { [s: string]: T; } | ArrayLike<T>): T[]; (o: {}): any[]; }
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/compiler/emptyObjectNarrowing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @strict: true
// @declaration: true

// Repro from #49988

declare function isObject(value: unknown): value is Record<string, unknown>;
const obj = {};
if (isObject(obj)) {
obj['attr'];
}