How can I get common types in the array

I have the structure

interface T1<T extends string> {
    path: T,
    handler: (value: T) => void
};

if I use a function with the same interface I don't have any problems with types, everything is correct

declare function foo1<T extends string>({ path, handler }: T1<T>): void

const handlerWithoutError = (v: 'some_path') => { }
foo1({ path: 'some_path', handler: handlerWithoutError })

const handlerWithError = (v: 'wrong_path') => { }
foo1({ path: 'other_path', handler: handlerWithError })

but if I try using an array instead of an obvious interface the type works in the wrong way

declare function boo1<T extends string>(arr: T1<T>[]): void

boo1([
    { path: 'some_path', handler: handlerWithoutError },
    { path: 'other_path', handler: handlerWithError },
])

I need the parameter of the "handler" function to match the "path" for each item of the array separately. For the second item it should be an error while the first isn't one. How to fix that problem and force TS to show correct tips ? Playground



Comments

Popular posts from this blog

I get wrong characters when retreiving the message body of an email using TIdIMAP4.UIDRetrieveTextPeek2()

How to drop the all the 1's in a correlation matrix

Today Walkin 14th-Sept