undefinedfix
Sign in

What is the meaning of the '=' assignment operation in a typescript generic?

SBRIGHT1 edited in Thu, 24 Mar 2022

describe

Take the chestnut of jQuery declaration file for example

interface JQueryStatic {
    // ...
    <TElement extends Element = HTMLElement>(selector: JQuery.Selector, context?: Element | Document | JQuery): JQuery<TElement>;
}

If it is < Telement extensions element > (selector: JQuery.Selector , context?: element | document | jQuery): jQuery < teleelement >, I understand it as saying: "you can pass in a generic parameter teleelement, and the teleelement needs to satisfy the constraints of the element type."

problem

  1. < tlement extensions element = HtmlElement >, what is the meaning of = HtmlElement here?
  2. The = assignment here is like the default value of a function parameter. Does it represent the default value of a generic? However, according to the official chestnut, type inference will automatically determine the type of confirmation T. should it not need the default value of generics?function identity<T>(arg: T): T {return arg;}let output = identity<string>("myString"); // type of output will be 'string'//Using type inference -- that is, the compiler will automatically help us determine the type of T based on the parameters passed inlet output2 = identity("myString"); // type of output will be 'string'
2 Replies
AIdrick
commented on Thu, 24 Mar 2022

Is the default value of generics.

In the example you quoted, it says

The compiler will automatically help us determine the type of T based on the parameters passed in

In jquerystatic, the parameters passed in have nothing to do with a dime of Telement. In other words, they cannot be inferred automatically. In this case, you can specify a default value.


In the example quoted by the subject, it is written "incoming parameter", so I use this lax expression, but it seems to lead to confusion

Here, the "incoming parameters" refer to the things in parentheses, that is, the formal parameters of the function. If there is no Telement in the parentheses, it is impossible to automatically infer the generics based on the actual parameters at the time of the call. As for the constraints of generics, they may or may not appear at the same time as the default values. There is no necessary connection between them.

Strictly speaking, the word "incoming" and the word "generic parameter" are incompatible. Generics are used at definition time and are removed at runtime.

Add another example:

function bare<TElement>(): TElement {
    return null as any as TElement;
}

const a = bare(); // typeof a === {}

function ext<TElement extends Element>(): TElement {
    return null as any as TElement;
}

const b = ext(); // typeof b === Element

function dft<TElement extends Element = HTMLElement>(): TElement {
    return null as any as TElement;
}

const c = dft(); // typeof c === HTMLElement

There is no Telement in the above parentheses.

oblus
commented on Thu, 24 Mar 2022

Thank you, big man. I'm done

lock This question has been locked and the reply function has been disabled.