Closed
Description
TypeScript Version: 3.3.333
Search Terms:
Type infer array nested interface structure
Code
interface ValueOnly {
value: number | null;
}
interface ValueAndKey {
key: string | null;
value: number | null;
}
interface ValueOnlyFields {
fields: Array<ValueOnly>;
}
interface ValueAndKeyFields {
fields: Array<ValueAndKey>;
}
interface BugRepro {
dataType: ValueAndKeyFields & ValueOnlyFields;
}
interface BugReproWithoutArray {
dataType: ValueAndKey & ValueOnly;
}
// This. Having the "key" errors out because of extra "key" field,
// while not having the "key" makes it complain that "key" is missing.
const repro: BugRepro = {
dataType: {
fields: [{
key: 'bla',
value: null,
}],
}
}
// Without nesting the type inside of an array, it can infer the type nicely.
const reproWithoutArray: BugReproWithoutArray = {
dataType: {
key: 'bla',
value: null,
},
}
Expected behavior:
Compiler should not complain that "key" is defined.
Actual behavior:
Compiler cannot identify that the array is neither Array, and Array without me casting it.
Related Issues:
Couldn't find.