(Typescript newbie warning)
I am creating a reusable reducer which takes a state and an action and returns the state, but is limited to accepting state which contains a certain type at a given key. This key is passed as a parameter of a function. If the state object that is passed does not contain the key that is passed, the compiler should raise an error.
Now, I got this working. However the error message generated by the compiler does not adequately, in my estimation, describe the problem. It doesn't say that "x property is missing on type", instead it gives other errors, which I will detail below.
Types
// FetchAction up here, not so relevant...
export type FetchState = {
status: FetchAction,
timestamp: Date
} | null
export type LoginState = {
token: string | null,
fetching: FetchState
};
Base Reducer
const intialState: LoginState = {
token: null,
fetching: null
}
const loginReducer: Reducer<LoginState> = (state = intialState, action) => {
//...other operations
return fetchingReducer(state, action, 'fetching');
}
Method 1
type FetchContainingState<S, K extends keyof S> = {
[F in keyof S]: F extends K ? FetchState : S[F];
};
export const fetchingReducer = <S extends FetchContainingState<S, K>, K extends keyof S>(state: S, action: Action, key: K): S => {
// implementation
}
This works properly. If I change the function call to:
return fetchingReducer(state, action, 'fetchin');
(misspelling fetching
), then I get this error:
Argument of type 'LoginState' is not assignable to parameter of type 'FetchContainingState'. Types of property 'token' are incompatible. Type 'string | null' is not assignable to type 'FetchState'. Type 'string' is not assignable to type 'FetchState'.
Well, it's good that it gives me an error. However, it just warns me about "token"
, or whatever other property exists on the object. It doesn't give me any direct indication as to which property it expects but doesn't exist.
Method 2
type EnsureFetchState<S, K extends keyof S> = S[K] extends FetchState ? S : never;
export const fetchingReducer = <S, K extends keyof S>(state: EnsureFetchState<S, K>, action: Action, key: K): S => {
// implementation
}
This works as well, when I change the call to return fetchingReducer(state, action, 'fetchin');
(misspelling "fetching"
), I get:
Argument of type 'LoginState' is not assignable to parameter of type 'never'.
More succinct, but even less descriptive of an error. It gives even less indication as to what could be wrong with the arguments that were passed.
Conclusion
In Method 1 I used a Mapped Type, and in Method 2 I used a Conditional Type to determine that the values for state
and key
that I passed did not pass the criteria we were searching for. However, in both cases the error messages do not really describe what the real problem is.
I'm new with more advanced types in Typescript, so there might be a really simple way of doing this or a simple concept that I've overlooked. I hope so! But anyways, the gist of this is: How can this type check on an object with a dynamic key be done more idiomatically, or in a way where the compiler generates a more beneficial error message?