I'm trying to learn more about Generics function in TypeScript and I'm stuck on a compiler error.
I wrote two same functions but each one in a different way,
// FIRST FUNCTION
function genericFunctionOne<T>(first: T): T {
return "Dummy return";
};
// SECOND FUNCTION
let genericFunctionOneBis: <T>(first: T) => T = first => "Dummy return";
the first one pass the compilator check but the second one does not.
ts/assign.ts(3,12): error TS2322: Type 'string' is not assignable to type 'T'.
Is that an expected behaviour ? I don't understand why one function passes the compilator check and not the other.
Here are the transpiled javascript:
// FIRST FUNCTION
function genericFunctionOne(first) {
return "Dummy return";
};
// SECOND FUNCTION
var genericFunctionOneBis = function (first) { return "Dummy return"; };