Just as static methods cannot access instance members, the static method cannot use the instance type argument.
For this reason, your static method must be generic and accept a type argument. I highlight this by using U in the static function, and T in the class. It is important to remember that the instance type of T is not the same as the static method type of U.
class Foo<T>
{
public static factory<U>(item: U): Foo<U>
{
return new Foo<U>();
}
instanceMethod(input: T) : T
{
return input;
}
}
You then call it by passing the type argument just before the parenthesis, like this:
var f: Foo<number> = Foo.factory<number>(1);
When type inference is possible, the type annotation may be dropped:
var f: Foo<number> = Foo.factory(1);
The variable f is an instance of Foo with a type argument of number, so the instanceMethod method will only accept a value of type number (or any).
f.instanceMethod(123); // OK
f.instanceMethod('123'); // Compile error