jcreamer898
jcreamer898

Reputation: 8189

Typescript ts-check javascript generics

Using TypeScript's new ts-check, I can't figure out how to handle working with a generic function.

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

The handleActions type comes from Definitey Typed and looks like...

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

So, I'm getting an error saying that...

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

Is there anyway to force the generic through?

Upvotes: 0

Views: 271

Answers (1)

rjustin
rjustin

Reputation: 1439

You can cast or type assert the object by adding the Type you want it to be in front with angle braces like this <IPoiState>{}.

Type Assertion

Upvotes: 1

Related Questions