-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
build: enable typescript sources #60489
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Be aware that in a TS file, these kinds of imports will be entirely untyped because
requireis just a function that returnsanyand is not special-cased in TS files where you're supposed to useimport util = require("internal/util").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.
Could the types be detected via something like
} = /** @type {import('internal/util')} */(require('internal/util'))? Perhaps using more explicit paths in the@type.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.
Technically yes, though it's casting
anyto something so would be "unsafe" (but very easily identifiably correct).Related-ish: microsoft/TypeScript#60032 (I can't find a better issue about
const = requirein TS, strangely)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.
The main reason not to do this is because it only gives you the value side of the import, so you can't use them in type positions.
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.
Sorry could you explain this in a bit more detail? Both what's wrong with my suggestion and what you would recommend instead, given the constraint that we can't use typescript syntax yet.
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.
Here's a fleshed out example of what happens in a TS file: Playground Link
This comes down to the fact that
requirethe function is "just a function" when in a TS file; the binder does not bind it in such a way that we can go look up types.We do this in JS files, though it is somewhat limited.
We've talked about doing this in TS files, but IIRC there are problems with doing so.
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.
Okay, but I was suggesting the JSDoc syntax in a .js file; you say that tsc will “do this“ in JS files, so would my suggestion work?
And if not, that’s fine, but is there something else you could recommend? Even if we migrate Node internals to TypeScript, that’s probably a very long timeline effort and so we’ll have a lot of JS files for a long time. Getting type checking working on JS files in the meantime would be a win.
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.
This PR is converting some of the JS to TS, which is why I was bringing it up in this TS file. If you leave things in JS, then you don't need to think about this, of course.
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 think we would like to add type checking to the Node codebase. Obviously that can be done by converting to TypeScript syntax, or adding JSDoc types to JavaScript; or a mix of both. We'll likely have a latter or a mix for a long time. Given that constraint, is there a way to add type checking to the files we have now?
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.
Yes, I'm sure you could use JSDoc (as clunky as it is).