I have a function that accepts an object of keys and each value has a type such that for each one the type of one of its fields determine the type for the other field. Code:
// We have this Alpha type and echo function...
type NoInfer<T> = [T][T extends unknown ? 0 : never]
interface Alpha<Foo extends string> {
foo: Foo
bar: `Depends on ${NoInfer<Foo>}`
}
declare const echo: <T extends string>(x: Alpha<T>) => void
echo({ foo: 'beta', bar: 'Depends on beta'})
// @ts-expect-error Trailing 2 is wrong
echo({ foo: 'beta', bar: 'Depends on beta 2'})
// Now we want a function (bravo) that takes a keyed index of Alphas...
declare const bravo: <T extends { [k: string]: Alpha<string> }>(xs: T) => void
bravo({
one: { foo: `1`, bar: `Depends on 1` },
// @ts-expect-error 1 !== 1x <-- fails
oneX: { foo: `1x`, bar: `Depends on 1` },
two: { foo: `2`, bar: `Depends on 2` },
// @ts-expect-error 2 !== 2x <-- fails
twoX: { foo: `2x`, bar: `Depends on 2` },
})
// how could this work?
As you can see from the "fails" comments I can make Alpha work initially but in the more complex object of Alphas I fail. Can you help me figure this out? Thanks!