Typescript: indexed access operator returns type unknown

我是研究僧i 提交于 2020-04-17 21:49:46

问题


follow up to this question.

It's possible to extract all possible types from a TS interface using indexed access operators.

In my case, I'm working w/ a large interface generated from a redux store/state. I tried the following: RootState[keyof RootState] but my resulted type is unfortunately of type unknown.

The RootState interface is exported from the store like this: export type RootState = ReturnType<typeof combineReducers({allMyReducers: ... ,... ,...})>;

I know that keyof RootState is a selection of strings of all my reducers/properties of the store but also of type symbol. Maybe this symbol type interferes with the RootState[keyof RootState] type? I don't know, this is just a guess, but if so, I would probably need to filter all the types that are strings (to exclude the symbol in that case), how would I do this?

Also, a question related to this about debugging this sort of problems. How can I view an expanded list of all types? I'm working w/ VS-Code and unfortunately, this is not possible w/ VS-Code atm as much as I know.

EDIT: found a solution to the debugging question.

The final type I have is the following:

type MyKeys = "along" | "list" | "ofAll" | "myKeys" | "andTheFollowingWeirdType" | typeof $CombinedState;

Notice the typeof $CombinedState. I'd say this is the only culprit that could lead to some troubles 🤔. How can I filter it out?

Also, I found where it's being declared in redux: declare const $CombinedState: unique symbol

EDIT 2:

I found "solution" 🥳. Turns out one of the reducers was not typed. And therefore caused the type to be interpreted incorrectly. But It's not really a solution because it means that a rootReducer of type "any" will break my use case, even though I think it should still work, shouldn't it?

来源:https://stackoverflow.com/questions/60077208/typescript-indexed-access-operator-returns-type-unknown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!