Skip to content

Inconsistent errors when assigning to property with index signature on object vs function #26647

@ghost

Description

TypeScript Version: 3.1.0-dev.20180823

Code

interface Idx {
    [key: string]: number;
}

interface Obj {
    idx?: Idx;
}
const l: Obj = {};
l.idx = {}; // Works

interface Fn {
    (): void;
    idx?: Idx;
}
const fn: Fn = () => {}; // Error: `{}` not assignable to `Idx`
fn.idx = {};

Expected behavior:

No error.

Actual behavior:

src/a.ts:15:7 - error TS2322: Type '{ (): void; idx: {}; }' is not assignable to type 'Fn'.
  Types of property 'idx' are incompatible.
    Type '{}' is not assignable to type 'Idx'.
      Index signature is missing in type '{}'.

Discovered in koa-morgan on DefinitelyTyped. Probably due to #26368.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already created

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions