I have this generic method
class Foo {
public static bar<T>(x: T): T {
...
if(x === null)
return null; //<------- syntax error
...
}
}
... //somewhere
const x = Foo.bar<number | null>(1);
I'm getting the syntax error
TS2322: Type 'null' is not assignable to type 'T'.
I'm expecting this to compile because T
could be null
.
what is the proper way to solve this problem
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…