TypeScript Version: 3.0.1
Search Terms:
string index signature, symbol, inference
Code
// returns all the values in `obj` whose property keys are strings
declare function getStringPropValues<T>(obj: {[x: string]: T}): T[];
// A unique symbol used in objects below
const SYM = Symbol();
// v1 is number[] as expected
let v1 = getStringPropValues({ foo: 42, bar: 51 });
// v2 is (string | number)[] as expected
let v2 = getStringPropValues({ foo: 42, bar: 'hello' });
// v3 is (string | number)[] <==== non-string key included in type inference
let v3 = getStringPropValues({ foo: 42, [SYM]: 'hello' });
// v4 is number[] <==== non-string key excluded from type constraint
let v4 = getStringPropValues<number>({ foo: 42, [SYM]: 'hello' });
Expected behavior:
v3 is number[], since the [SYM] key is not part of the string index signature.
Actual behavior:
v3 is (string | number)[], implying that the [SYM] key belongs to the string index signature.
v1, v2 and v4 all behave as I would expect. Note in v4 the type parameter is given explicitly, and the [SYM] key is correctly excluded from the type constraint since it is not part of the string index signature. But this seems inconsistent with the behaviour of v3 where it is treated as if is does belong to the string index signature.
TypeScript Version: 3.0.1
Search Terms:
string index signature, symbol, inference
Code
Expected behavior:
v3isnumber[], since the[SYM]key is not part of the string index signature.Actual behavior:
v3is(string | number)[], implying that the[SYM]key belongs to the string index signature.v1,v2andv4all behave as I would expect. Note inv4the type parameter is given explicitly, and the[SYM]key is correctly excluded from the type constraint since it is not part of the string index signature. But this seems inconsistent with the behaviour ofv3where it is treated as if is does belong to the string index signature.