Closed as not planned
Description
π Search Terms
"not assignable",
"parametized argument",
"conditional return"
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about 5.1.6
β― Playground Link
π» Code
interface Identifiable { id: string; name?: string; }
function myfunc<TIdt extends Identifiable | null>(infos: TIdt): TIdt extends Identifiable ? string : null {
if (infos == null) {
return null;
//Err: Type 'null' is not assignable to type 'TIdt extends Identifiable ? string : null'.(2322)
} else {
return infos.id;
//Err: Type 'string' is not assignable to type 'TIdt extends Identifiable ? string : null'.(2322)
}
}
let tmp = myfunc(null);
//=> let tmp: null
let tmp2 = myfunc({name: '', id: ''});
//=> let tmp2: string
π Actual behavior
tsc
doesn't seem to match the if
statement with the conditional return.
On the other hand it understand what I want to say when calling the method.
π Expected behavior
What I expect is for typescript to understand in the if that if the argument is null
then I can only return null
, and alse only return a string
, like defined in the function signature.
Additional information about the issue
I have the impression that the signature is correct as typescript infer correctly when calling the function, so the problem is inside the function with the if
?