Skip to content

Intersection of empty object type with empty object type is missing index signature #20225

@magnushiie

Description

@magnushiie

TypeScript Version: 2.7.0-dev.20171122

Code

type Dictionary = { [name: string]: string };

const intersectDictionaries = <F1 extends Dictionary, F2 extends Dictionary>(
  d1: F1,
  d2: F2,
): F1 & F2 => Object.assign({}, d1, d2);

const testDictionary = <T extends Dictionary>(_value: T) => { };

const d1 = {};
testDictionary(d1); // OK, d1 is inferred to be {}
const d2 = intersectDictionaries(d1, d1);
testDictionary(d2); // error, d2 is inferred to be {}
// compile error on the above line:
// ts-braces-not-assignable.ts(13,16): error TS2345: Argument of type '{}' 
//  is not assignable to parameter of type 'Dictionary'.
//   Index signature is missing in type '{}'.

const d3 = {
  s: '',
};
testDictionary(d3); // OK
const d4 = intersectDictionaries(d1, d3);
testDictionary(d4); // OK
const d5 = intersectDictionaries(d3, d1);
testDictionary(d5); // OK
const d6 = intersectDictionaries(d3, d3);
testDictionary(d6); // OK

Expected behavior:
No error

Actual behavior:
Error:

ts-braces-not-assignable.ts(13,16): error TS2345: Argument of type '{}' is not assignable to parameter of type 'Dictionary'.
  Index signature is missing in type '{}'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions