I am trying to understand how TypeScript conditional type works. Here is my code. There are type errors:
interface MyType {
name: string;
}
const testFunc = <T extends MyType | string>(
what: T
): T extends MyType ? MyType : string => {
if (typeof what === 'object') {
return what['name'];
}
return what;
};
What is the correct usage?


I'd do it like this:
as anymeans "TypeScript, don’t complain about this type". The problem is that the narrowed type ofwhatis not picked up by the conditional type, so the function cannot evaluate the condition and narrow the return type towhat.