Closed
Description
TypeScript Version: 3.0.0-dev.20180607
Search Terms:
Index signature missing inferred mapped types
Code
declare function map<FromValue>(
from: Publisher<FromValue>,
to: (props: FromValue) => any
);
declare function combine<CombinedValue>(children: {
[K in keyof CombinedValue]: Publisher<CombinedValue[K]>
}): Publisher<CombinedValue>;
type Publisher<Value> = { value: Value; }
type Box<Value> = { data: Value }
type X = { a: string }
type Y = { b: number }
// ---
let a = combine({
x: undefined as Publisher<Box<X>>,
y: undefined as Publisher<Box<Y>>,
})
map(a, boxes => {
// Index signature if missing in type { x: Box<X>, y: Box<Y> },
let fail1: {
[name: string]: Box<any>
} = boxes
// But not when type { x: Box<X>, y: Box<Y> } is specified manually
// instead of inferred
let pass1: {
[name: string]: Box<any>
} = boxes as {
x: Box<X>;
y: Box<Y>;
}
// And it can still be assigned to index-typed objects...
let pass2: {
[name: string]: any
} = boxes
})
// Here `b` has the same type as the type inferred for `a`, but it works.
// The behavior must stem from inferred type.
let b: Publisher<{
x: Box<X>,
y: Box<Y>,
}>
map(b, boxes => {
let pass3: {
[name: string]: Box<any>
} = boxes
})
Expected behavior:
Should compile.
Given that boxes
can be assigned to { [name: string]: any }
, it feels like it should also be able to be assigned to { [name: string]: Box<any> }
.
Actual behavior:
Fails with:
test.ts:24:7 - error TS2322: Type '{ x: Box<X>; y: Box<Y>; }' is not assignable to type '{ [name: string]: Box<any>; }'.
Index signature is missing in type '{ x: Box<X>; y: Box<Y>; }'.
24 let fail1: {
Playground Link:
Related Issues: