About typescript using class type syntax new ()?

when using ts, I see this syntax, but I don"t understand the benefits of this definition and why?

function create<T>(c: {new(): T; }): T {
    return new c();
}

class BeeKeeper {
    hasMask: boolean;
}

class ZooKeeper {
    nametag: string;
}

class Animal {
    numLegs: number;
}

class Bee extends Animal {
    keeper: BeeKeeper;
}

class Lion extends Animal {
    keeper: ZooKeeper;
}

function createInstance<A extends Animal>(c: new () => A): A {
    return new c();
}

createInstance(Lion).keeper.nametag;  // typechecks!
createInstance(Bee).keeper.hasMask;   // typechecks!
Mar.04,2021

look at handbook also encountered the same problem, do not understand the syntax of ts can not understand.
there is no detailed definition of handbook on the official website, and there is no link to spec on the official website.

TypeScript Language Specification
https://github.com/Microsoft/.

< hr >

c: the 'new'' in {new (): T} is the 'new'' in the new c () under Constructor Type Literal, and the 'new'' is the difference between the two.

c: {new (): T} and c:new () = > T are the same, and the latter is an abbreviation for the former, meaning that the type of C is an object type and the object contains a constructor whose return type is T.

Note that':'is followed by Type Information, where the'= >'is not arrow function, but is used to indicate the return type of the function.

3.8.9 Constructor Type Literals

A constructor type literal specifies the type parameters, regular parameters, and return type of a construct signature.

ConstructorType:
newTypeParametersopt(ParameterListopt)=>Type

A constructor type literal is shorthand for an object type containing a single construct signature. Specifically, a constructor type literal of the form

new < T1, T2, ... > ( p1, p2, ... ) => R

is exactly equivalent to the object type literal

{ new < T1, T2, ... > ( p1, p2, ... ) : R }

Note that constructor types with multiple construct signatures cannot be written as constructor type literals but must instead be written as object type literals.

if you don't use new (): T you can try to write this function out.

The

create function takes an Class, return value that is an instance of this Class.

c: t means that the type of c is T, but the purpose of this function is not to require the type of c to be T, but to require c to be T.

try to compare:


:<br>`function showData(data){

return data;

}
showData ('abc'). Length;
showData (12). Length;// writes js without reporting errors but undefined
showData ({name:' leo'}). Length;
showData ([125,8])) .length; `
We hate that the code has no syntax errors but the final result is incorrect.
using generics allows the writing stage to directly report syntax errors instead of syntax errors, with the following code:

function showData<T>(data:T){
    return data;
}
console.log(showData<string>('abc').length);
console.log(showData<number>(12).length);
console.log(showData({ name: 'leo' }).length);
console.log(showData([12, 5, 8]).length);

A grammatical error is not compiled into js, so that it can stifle the error in the cradle

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf9b12-3191a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf9b12-3191a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?