Search code examples
typescriptfunctiontypescript-genericsterminologylate-binding

What is it called when a function has a late-binding generic type?


What is it called when a function has a late-bound Generic type (which is determined only when the function is called, not when it is defined).

It is shown as LateT in the code example below, and contrasted with the EarlyT early-bound case where the type of the function reference itself already has the typed constraint (and it's not late-bound when the function call is evaluated).

I am trying to find documentation for this style of function definition and to describe it as part of an API and I am struggling to reference the right part of the Typescript documentation without the correct distinguishing terms.

type Op<EarlyT> = (item:EarlyT) => void

type LateOp = <LateT>(late:LateT) => void;

Solution

  • What you're calling a "late-bound generic" is referred to in TypeScript as a generic function or call signature, while what you're calling an "early-bound generic" is referred to in TypeScript as a generic type.

    The relevant documentation can be found in the TypeScript Handbook section on generic types, where a type like LateOp "describes a generic function" while a type like Op is a "non-generic function signature that is a part of a generic type".


    So in

    type GenFunc = <T>(x: T) => void;
    

    the GenFunc type is non-generic (it has no generic type parameter) but it refers to a generic call signature (it has a generic type parameter), while in:

    type GenType<T> = (x: T) => void
    

    the GenType type is generic (it has a generic type parameter) but it refers to a non-generic call signature (it has no generic type parameter itself, although the type parameter from the generic type is still in scope).


    See TypeScript how to create a generic type alias for a generic function? for some more discussion about the relationship between generic functions and generic types.