-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
TypeScript Example (CJS) #4942
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
base: 5.0
Are you sure you want to change the base?
TypeScript Example (CJS) #4942
Conversation
Nice, seems to have passed |
examples/typescript/index.ts
Outdated
const app: import("express").Application = module.exports = express(); | ||
|
||
app.get('/', function(request, response) { | ||
console.log(request.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Segmentational, can you use the types here? like Request and Response types, also the import import express from 'express';
instead of the require function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, I can clean this up!
examples/typescript/index.ts
Outdated
@@ -0,0 +1,13 @@ | |||
const express = require("express"); | |||
|
|||
const app: import("express").Application = module.exports = express(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using this statement? you can easily set app value like this const app = express();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking from memory, I believe it was due to the narrowing and the strictness of using the @types/express
npm
package. Overall, it's to my understanding there's a few issues with @types/expess
.
Again speaking from memory, I thought I had read in the documentation that express
is 100% http
compliant (relating to the built-in node.js library); however, there's loss of the exposed HTTP.Server
types. Example
import express from "express";
import type HTTP from "http";
import type { Express } from "express-serve-static-core";
const Application: Express & HTTP.Server = express();
Does not allow compilation.
Regardless, I certainly can simplify
examples/typescript/index.js.map
Outdated
@@ -0,0 +1 @@ | |||
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAEnC,MAAM,GAAG,GAAkC,MAAM,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC;AAEtE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,UAAS,OAAO,EAAE,QAAQ;IACnC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAEzB,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AACjC,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAEjB,OAAO,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC","sourcesContent":["const express = require(\"express\");\n\nconst app: import(\"express\").Application = module.exports = express();\n\napp.get('/', function(request, response) {\n console.log(request.url);\n\n response.send(\"Hello World\");\n});\n\napp.listen(3000);\n\nconsole.log('Express started on port 3000');\n"]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this file should not be released, is the build result? same for the file tsconfig.build.info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100% - I'll clean up
examples/typescript/index.js
Outdated
console.log(request.url); | ||
response.send("Hello World"); | ||
}); | ||
app.listen(3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the index file cloned?
@bozzelliandrea, thank you for the feedback; I've since updated with your recommended changes, and point of replacing Sorry my response took so long, I'll try and get back sooner next time around P.S. I updated the Using |
- Updated `README.md` to better reflect context - Removed unnecessary instruction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Segmentational everything looks ok and clean, nice work! 😃
Just my curiosity, what does CJS mean?
@bozzelliandrea, sorry for the delayed response... I just moved halfway across the united states, so that took a little time haha. Yeah, " I'm unsure of the exact other places, but essentially it's a shorthand sometimes used to describe the |
The following PR relates to a simple TypeScript implementation using
express@5.0
.Additionally, example (found in
examples/typescript
) includes atsconfig.json
that compiles down toES5
JavaScript. A merge request I made previously had failed some tests relating to version limitations (node), so this should handle that case.However, the compiled JavaScript and other generated assets are set to be tracked via VCS (
git
). It's of normal operation to usually untrack them via including*.js
in the.gitignore
.We can later add a
.gitignore
in the example folder itself. But I do want to see what the GitHub actions returns with. If appropriate, I'll update/discuss changes to these tests to account for the typescript example.Discussion
Looking at the other example folders, often they are without
README.md
s,package.json
s, etc. etc.While totally fine, the example this PR relates to, as well as another I have outstanding, perhaps could be exceptions. I know the package maintainers and other contributors see, almost daily, issues & questions relating to ESM, TypeScript, and even both.
Providing examples, like this one, hopefully will make everyone's life a bit easier
:D
.