2

I have the following types:

interface IFilterField <T extends FilterFieldValue > {
    defaultValue: T
    displayStr?: string
}

interface IFilterFields = {
   costMax: IFilterField<number>
   costMin: IFilterField<number>
   .....
}>

When I try to type an object that will link between the filterField and the type of its` value, this works:

export type IFiltersState<FiltersKeys extends keyof IFilterFields> = {
    [K in keyof FiltersKeys]: IFilterFields["costMin"|"costMax"]["defaultValue"]

}

But this fails:

export type IFiltersState<FiltersKeys extends keyof IFilterFields> = {
   [K in keyof FiltersKeys]: IFilterFields[K]["defaultValue"]
}

Even though the constraint is pretty much the same.

Error:

[ts] Type 'K' cannot be used to index type 'IFilterFields'. [2536]
[ts] Type '"defaultValue"' cannot be used to index type 'IFilterFields[K]'. [2536]

Why is this error occuring?

How can I acheive the purpose of a mapped type, which follows this principles: A new type T2 has some of the keys of type T1, and for each such key typeof T1[key] equals typeof T2[key][SomeFieldName]?

2 Answers 2

2

I found my error. Since FiltersKeys is already a union of keys/strings, there is a reduntand keyof in [K in keyof IfilterFields].

Sign up to request clarification or add additional context in comments.

1 Comment

Should I delete this question altogether?
1

Because FiltersKeys is extended from IFilterFields, therefore it could contain different keys. You can use

export type IFiltersState<FiltersKeys extends keyof IFilterFields> = {
    [K in keyof IFilterFields]: IFilterFields[K]["defaultValue"]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.