打字稿ts-check javascript泛型

时间:2017-09-27 15:41:53

标签: javascript typescript definitelytyped

使用TypeScript的新ts-check,我无法弄清楚如何处理泛型函数。

/**
 * @type {Reducer<IPoiState, any>}
 */
const poi = handleActions({
  [ADD_BOOKMARK_START]: (state) => {
    return { ...state };
  },
}, {});

handleActions类型来自Definitey Typed,看起来像......

export function handleActions<State>(
    reducerMap: ReducerMap<State>,
    initialState: State
): Reducer<State, any>;

所以,我收到的错误是......

Type 'Reducer<{}, any>' is not assignable to type 'Reducer<IPoiState, any>'.
  Type '{}' is not assignable to type 'IPoiState'.

无论如何都要强制通用吗?

1 个答案:

答案 0 :(得分:1)

你可以通过像<IPoiState>{}这样的角括号添加你希望它在前面的Type来强制转换或输入断言。

Type Assertion