Skip to content

Behavior difference using || {} with unknown #1563

@benjaminjkraft

Description

@benjaminjkraft

Steps to reproduce

function f() {
  const v: unknown = "lol";
  const acceptsRecord = (record: Record<string, string>) => {};
  acceptsRecord(v || {});
}

Behavior with [email protected]

src/record.ts:4:17 - error TS2345: Argument of type '{}' is not assignable to parameter of type 'Record<string, string>'.
  Index signature for type 'string' is missing in type '{}'.

4   acceptsRecord(v || {});
                  ~~~~~~~

Behavior with [email protected]

no error

Commentary

I think in principle tsc is correct here, because {} includes primitives, { asdf: 3 }, etc. But note that both tsc and tsgo allow this if we do const v: {} = "lol";! So maybe this is some intentional case of playing fast and loose with inexactness, and tsgo is more consistent about it?

Metadata

Metadata

Labels

Domain: Type CheckingRelated to type checking, grammar checkingbugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions