Skip to content

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

Open
wants to merge 5 commits into
base: 5.0
Choose a base branch
from
Open

Conversation

Segmentational
Copy link

The following PR relates to a simple TypeScript implementation using express@5.0.

Additionally, example (found in examples/typescript) includes a tsconfig.json that compiles down to ES5 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.mds, package.jsons, 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.

@Segmentational
Copy link
Author

Nice, seems to have passed

const app: import("express").Application = module.exports = express();

app.get('/', function(request, response) {
console.log(request.url);

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.

Copy link
Author

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!

@@ -0,0 +1,13 @@
const express = require("express");

const app: import("express").Application = module.exports = express();

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();

Copy link
Author

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

@@ -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"]}

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

Copy link
Author

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

console.log(request.url);
response.send("Hello World");
});
app.listen(3000);
Copy link

@bozzelliandrea bozzelliandrea Jul 5, 2022

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?

@Segmentational
Copy link
Author

@bozzelliandrea, thank you for the feedback; I've since updated with your recommended changes, and point of replacing require with import.

Sorry my response took so long, I'll try and get back sooner next time around :D.

P.S. I updated the tsconfig.json and package.json to align much closer to what a standard, default configuration would look like. Notice, too, the latest package version reference(s).

Using latest is to avoid (haha hopefully) any technical debt down the road with issues coming through asking to update the typescript example.

Segmentational and others added 2 commits July 15, 2022 01:38
- Updated `README.md` to better reflect context
- Removed unnecessary instruction
Copy link

@bozzelliandrea bozzelliandrea left a 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?

@Segmentational
Copy link
Author

@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, "CJS" can be seen in a few odd places in the node official documentation. The first example, although referenced as a file extension, can be seen here:

I'm unsure of the exact other places, but essentially it's a shorthand sometimes used to describe the commonjs javascript module type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants