I've got a type that TypeScript can't infer the generic of.
interface Foo<A> {
[name: string] : {
foo : A
}
}
function makeFoo<A>(foo: Foo<A>) : Foo<A>{
return foo
}
// Works fine when manually specifying the types
const manuallyTyped : Foo<string | number> = {
a: {
foo: '1'
},
b: {
foo: 3
}
}
// ERROR, Can't infer type as Foo<string | number>
makeFoo({
a: {
foo: '1'
},
b: {
foo: 3
}
})
Originally, I was using the type below but I wanted to make the values of the object objects themselves. Inference works just fine when the indexed signature is flat.
interface FlatFoo<B> {
[name: string] : B
}
function makeFlatFoo<B>(bar: FlatFoo<B>): FlatFoo<B>{
return bar
}
// Correctly has type FlatFoo<string | number>
const inferred = makeBar({
a: 'a',
b: 2
})
Does anyone have an explanation and/or a recommendation for getting this to work?