microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.77k stars 12.35k forks source link

Undocumented = operator syntax in Generic Constraining #30337

Open Ethan-Arrowood opened 5 years ago

Ethan-Arrowood commented 5 years ago

In the declaration file for Fastify (https://github.com/fastify/fastify/blob/master/fastify.d.ts) there is some TypeScript code I do not recognize and cannot find another example of in the TypeScript documentation:

declare function fastify<
  HttpServer extends (http.Server | http2.Http2Server) = http.Server,
  HttpRequest extends (http.IncomingMessage | http2.Http2ServerRequest) = http.IncomingMessage,
  HttpResponse extends (http.ServerResponse | http2.Http2ServerResponse) = http.ServerResponse
>(opts?: fastify.ServerOptions): fastify.FastifyInstance<HttpServer, HttpRequest, HttpResponse>;

I recognize the Generic constraint syntax with the extends keyword, but what about the = http.Server part? Is this like a default parameter or something?

Whatever this syntax means it should be better documented.

I tried googling for this syntax by searching

RyanCavanaugh commented 5 years ago

I agree this should be covered by the handbook, though it is discoverable through the search you tried

image

image

Ethan-Arrowood commented 5 years ago

Ahh i see. I was looking at the wrong documentation. I think what deterred me from this page originally was that it was v2.3 not v3. I don't like to read different major version docs because it can have unsupported information

rossng commented 4 years ago

It's near impossible to find out what this syntax does if you haven't already guessed that it's for generic parameter defaults. The syntax is un-Googleable. I had to use an AST browser to figure out what was going on!

A vote from me for this to be documented on the Generics page.

HartS commented 3 years ago

4 years after this syntax was introduced, I had to ask in the Typescript discord to get an answer to this. As mentioned by @rossng, completely impossible to google, or find from the TS docs.