Closed
Description
TypeScript Version: 2.7.1
Search Terms: infer
Code
interface IFooOptions<K extends string> {
name?: string;
value?: number;
allFoos?: {[key in K]?: boolean }
fn?: (allFoos: {[key in K]: string }) => void;
}
class Bar<T> {
constructor(foos: {[key in keyof T]: IFooOptions<keyof T> }) {
// ...
}
}
const bar = new Bar({
firstFoo: {
name: "john",
value: 3,
allFoos: { // here allFoos infered
firstFoo: true,
nonexist: 'yep'
}
},
secondFoo: {
name: 'sandra',
fn: allFoos => { // but here allFoos is any though the type of "fn" is infered correctly
allFoos.nonexist = 2
}
}
});
const myFoo: IFooOptions<'one' | 'two'> = {
name: 'adas',
fn: allFoos => { //allFoos is infered right
allFoos.nonexist = 2
}
}
Expected behavior:
The allFoos
argument inside fn
field in the new Bar
argument should have a type of
{
firstFoo: string
secondFoo: string
}
Actual behavior:
allFoos
type is implicit any
Also it repoduces without using a class, just with plain generic function
Metadata
Metadata
Assignees
Labels
No labels