We need to type the function, which requires a list of keys (strings) and produces object with the same keys where values are string | undefined when key starts with ? or otherwise just string.
export function useParams<T extends string>(
expected: T[]
): { [key in T as NonOptional<T>]: string } {
// ... our custom implementation
return null as any;
}
type NonOptional<T extends string> = T extends `?${infer Inner}` ? Inner : T;
// Should produce type { a: string; b: string | undefined }
const result = useParams(['a', '?b']);
However, I'm not sure how to type the output.