Checking out https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
I'm curious as to why this code is not functioning properly?
interface IdLabel {
id: number
}
interface NameLabel {
name: string
}
type NameOrId<T extends number | string> = T extends number
? IdLabel
: NameLabel;
function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
if (typeof idOrName === 'number') {
return { id: 1 }
} else {
return { name: 'foo' }
}
}
On the other hand, this code snippet does work according to the documentation example provided. However, it seems pointless as it does not actually return any value...
function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
throw "unimplemented";
}
Is it possible that conditional types only function with "type definition"..?