'TypeScript failing to infer parameter types
I'm initialising an object with a function property, and TypeScript is failing to infer the types of the parameters despite the tooltip indicating that the types for that property are known.
I'm trying to initialise the object like this:
const myObject: MyInterface<number> = {
...
set: (value, args) => {}
}
The interface for this is defined like this:
export interface MyInterface<T> extends SomeOtherInterface<T> {
set: (value: T, args: SetOptions) => void;
}
(I've changed the names of interfaces to make it easier to understand without context)
I get the error Parameter 'value' implicitly has an 'any' type and Parameter 'args' implicitly has an 'any' type with this. If I hover over the set property in myObject, the tooltip correctly identifies the type definition as (property) MyInterface<number>.set: (value: number, args: SetOptions) => void so I'm struggling to see why TypeScript can't infer the types of the arguments?
I can explicilty define the argument types, however since they are being infered as any this would not be good - If the method signature of this changes, it won't trigger any compile time errors.
Does anyone know how to get TypeScript to correctly identify the types in this case?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


