Reputation: 763
We have a pure component, and we are trying to convert this to the Flow type safety. The application is using this component as a HOC ( High-Order Component). It is generating a context and injecting to the dispatched components.
Therefore, one method of the HOC is returning an object literal which is involving many binding operations. In this key-value pairs, there is one expression we didn't handle.
We are getting an error about missing notation:
Missing type annotation for T. T is a type parameter declared in array type [1] and was implicitly instantiated at call of method filter [2].
export type PropsType = {
reviewConf ? : Object,
...
}
export type ContextType = {
registerComponent: () => void,
errors ? : Array < any >
...
}
export type StateType = {
meta: Object
}
class AbstractPureFormComponent extends React.PureComponent < PropsType, StateType > {
constructor(props: PropsType, context: ContextType) {
super(props, context)
this.state = {
meta: {}
}
}
getChildContext() {
return {
registerComponent: this.registerComponent.bind(this),
...
errors: Object.keys(this.state.meta).filter(
name => this.state && this.state.meta[name].error
)
}
}
}
}
So what is the best practice for typing this error:
key? Should it be an interface or type or something else...
Upvotes: 11
Views: 6901
Reputation: 1
The adjust is simpler than it, the problem is your variable is "array: Array<MyArray>
", and "sort" return "array: MyArray[]
", so, just change your variable to "array: MyArray[]
".
Upvotes: -1
Reputation: 17573
I just encountered this issue with a function that simply clones and sorts an array, e.g.
const sortArray = (myArr: MyArray[]) => [...myArr].sort((a, b) => a > b ? -1 : 1)
What solved it for me was simply typing the return value of sortArray
:
const sortArray = (myArr: MyArray[]): MyArray[] => [...myArr].sort((a, b) => a > b ? -1 : 1)
Upvotes: 14