Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undocumented = operator syntax in Generic Constraining #30337

Open
Ethan-Arrowood opened this issue Mar 12, 2019 · 4 comments
Open

Undocumented = operator syntax in Generic Constraining #30337

Ethan-Arrowood opened this issue Mar 12, 2019 · 4 comments
Labels
Docs The issue relates to how you learn TypeScript

Comments

@Ethan-Arrowood
Copy link

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

  • typescript generic constraint
  • typescript generic constraint assignment
  • typescript generic defaults
@RyanCavanaugh
Copy link
Member

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

image

image

@RyanCavanaugh RyanCavanaugh added the Docs The issue relates to how you learn TypeScript label Mar 12, 2019
@Ethan-Arrowood
Copy link
Author

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
Copy link

rossng commented Aug 12, 2020

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
Copy link

HartS commented Apr 30, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants