Skip to content

Circular record cannot be assigned an empty record #34690

Closed
@niedzielski

Description

@niedzielski

TypeScript Version: 3.7.0-beta

Search Terms: recursive empty record

Code

const foo: Record<never, never> = {} // OK

interface RecordOfRecords extends Record<keyof any, RecordOfRecords> {}
const recordOfRecords: RecordOfRecords = {}
recordOfRecords.propA = {...(foo !== undefined ? {foo} : {})} // OK
recordOfRecords.propB = {...(foo && {foo})} // OK
recordOfRecords.propC = {...(foo !== undefined && {foo})} // Not OK
// ^Error:
//  Type '{} | { foo: Record<never, never>; }' is not assignable to type 'RecordOfRecords'.
//    Type '{}' is not assignable to type 'RecordOfRecords'.
//      Index signature is missing in type '{}'.(2322)

interface RecordOfRecordsOrEmpty extends Record<keyof any, RecordOfRecordsOrEmpty | {}> {}
const recordsOfRecordsOrEmpty: RecordOfRecordsOrEmpty = {}
recordsOfRecordsOrEmpty.propA = {...(foo !== undefined ? {foo} : {})} // OK
recordsOfRecordsOrEmpty.propB = {...(foo && {foo})} // OK
recordsOfRecordsOrEmpty.propC = {...(foo !== undefined && {foo})} // OK

Expected behavior: Empty objects are always assignable to Records.

Actual behavior: Empty objects are only assignable to the non-recursive records.

Playground Link

Related Issues: #25437

Edit: sorry, but I couldn't find this issue in the tracker although I'm almost certain it must exist. If possible, please link this to any existing (possibly closed) issue.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions