I'm not sure if this is a compiler bug or if I'm just incapable of proper typing. I'm working on generating an API client for some route specifications. I whittled it down to this repro.
type Methods = "get" | "delete" | "post" | "put"
interface TestOp<TOp extends string> {
id: TOp
}
type TestPath<TOps extends string = string> = Partial<{
[TMethod in Methods]: TestOp<TOps>
}>
function useTestPath<TOps extends string, TP extends TestPath<TOps>>(path: TP) {
return path
}
const tp = useTestPath({
get: { id: "hello" },
put: { id: "goodbye"}
})
type OpKeys<TPath extends TestPath> = TPath[keyof TPath]["id"]
type ok = OpKeys<typeof tp>
OpKeys gets the error Type '"id"' cannot be used to index type 'TPath[keyof TPath]'
Which feels wrong. But what's more interesting is VSCode is able to give the type hint that ok is infact "hello" | "goodbye"
Ultimately I'm trying to get a mapped object of functions that have the names of id, but this weird indexing issue is blocking me. Maybe I did something wrong with the <TOp extends string> stuff. But it's the only way I found get useTestPath to properly infer the id as literals and not string.