-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Typescript should support --lib node #9466
Comments
You are raising multiple items here. First, supporting node typings more "first class" will be occurring with TypeScript 2 and the @typings. Because type acquisition is hard, this is why the TypeScript team have been working to make it easier. TS2 needs to be released and the documentation, etc. will need to be flushed through.
That seems to be a wider community issue. While there is no plain Node.js tutorial on the official website, I am sure the TypeScript team would gladly accept a submission to the tutorials repo. At the end of the day, TypeScript will be only one part of an larger ecosystem. As the design goals for TypeScript state it is a non-goal to:
Therefore, even when targeting a browser, TypeScript is part of a solution, but is seldom the full solution.
Like what? What helpful information could be provided? It is informing you already that it cannot find the module you are looking for. I think you biggest "gripe" is that by default it isn't easy to switch from "assume a browser" to "assume a Node.js environment". Again that will be addressed in TS2... it should essentially be as simple as: > tsc --init index.ts
> npm init
> npm install @typings/node --save Note, I haven't tried it yet and it is my limited understanding of how type acquisition is changing in TS2, as I can't remember if |
I agree with some of your points. Basically typescript already understands browser types because it has a pre-set built of libs e.g.
Having node as one of the libs makes sense since node is just another engine. It also has
Which means typescript is node aware at some level. It just doesn't know what node types are. I am arguing that node is an equal host to the browser host therefore its definition should be included and should be a compiler flag away. I should be able to take an existing node project and rename all js files to .ts, add --lib node and it should work. It shouldn't complain about core modules not being found. |
Hey guys, I was actually pushing for a However, after discussing it with @bowdenk7, we're a bit of the opinion that All of that said, I would like to see if people hold the opinion that the convenience brought from this fix completely outweighs that problem. If that was the case, we could make our |
My observations with using Browser Project Setup
Node.js Project Setup (with
|
I'm fine with either typings or --lib as long as there is a consistent way Back in the day when I used eclipse with java, it would be smart to I'd really love to see the language service offer hints of fixing e.g
I'd say it would be sweet if I could just type away imports and it would 2nd example
We live in an age of artificial intelligence. Its bizzare that I have to go On Friday, July 1, 2016, Daniel Rosenwasser notifications@github.com
|
The argument that TypeScript should treat NodeJS native modules like any other library is akin to AWB's tweet that NodeJS should not be considered a platform in the way that browsers are. Whereas
I would a agree that maintaining |
@DanielRosenwasser I understand the design to move this to |
I think typescript project should not maintain the lib files. Instead it That means as a user, typescript will work with browser and node out of the Making changes and maintaining the d.ts files can be done in their On Wednesday, July 13, 2016, Kenneth Brubaker notifications@github.com
|
@clavecoder I am tone deaf now? You could have offered solutions to peoples problems, but no, you would rather complain that I am tone deaf. People do take some things personally. |
@kitsonk, to me it would be an honor to be equated in any way with @allenwb, who has given so much to the software industry and to the JavaScript community in particular. You, @ahejlsberg, and the entire TypeScript team are similarly improving the lives of JavaScript coders in countless ways. So, no, you are not tone deaf. I attribute AWB's lack of seeing the importance of Node to the mind numbingly fast pace of change of the JavaScript developer experience. Likewise, whereas, TypeScript's original goals were to be a wrapper as thin as possible around JavaScript, it's rise to prominence as a fundamental tool of the development of, by far, the most popular JavaScript development framework, makes it hard not to consider it's role in the developer experience. Bower's star has faded precisely because npm has become, again by far, the dominant library package management registry on the planet. This has made And I do most humbly thank you and the entire TypeScript team for their hard work and extraordinary contribution to the JavaScript community. |
To avoid any confusion, I am just a member of the wider community, not part of the TypeScript team. I am the project lead for Dojo 2 which has a vested interest in the roadmap of TypeScript. |
@DanielRosenwasser and TypeScript team, it looks as though the Node.js Foundation folks are interested in working with you: nodejs/Release/issues/123. You may want to contact @rvagg, @nebrius, or @joshgav to see how they could work with you. |
@nojvek, it, perhaps, should be an option of |
Npm has a notion of peerDependencies. I would expect typescript to have a default set of typings peerDependencies This means, there are no extra steps to get node working. It works out of If a Dev wants to change versions of the libs, he just npm installs that Typescript team also no longer has to worry about maintaining node.d.ts. I feel this is a clean solution and npm does most of the work. Thoughts? On Thursday, July 14, 2016, Kenneth Brubaker notifications@github.com
|
peerDependencies are deprecated and have been partially removed from npm 3, so that's a non-starter I'm afraid |
npm3 installs in a flat structure so just having dependencies should work On Thu, Jul 14, 2016 at 11:00 AM, Bryan Hughes notifications@github.com
|
It doesn't always install flat. If module |
Correct. npm3 flattens by default, but has a fairly complex way of dealing with version conflicts. This is what happens when you build something that has unlimited, low cost access to the local filesystem, like Node.js does, not like how a browser does. This also leads to building a loader that doesn't allow remapping of packages, because again, relatively costless access to the local filesystem. Of course, this was attempted to be solved by CommonJS and AMD being all part of a single loader specification back a decade ago with James Burke trying hard to get those of Node.js/CommonJS to have a single loader, but like all good points of hubris, people couldn't agree and so we ended up with CJS and AMD, only a decade later still find ourselves tripping over the same problems. |
I really don't understand why you think this will magically happen. If the TypeScript team doesn't maintain them, then who does? What version of Node.js should be installed? The version it is running under, is that the final target for the consumable code? Should it be installed globally so that every project has the same typings? How does the developer identify a project is for a browser or for node? What about if they are targeting Rhino, or Electron? What about the @types with 2.0 do you not like? Pretty much everything you are talking about seems to be essentially that, though in a real maintainable way. |
Node.d.ts is currently maintained by a third party. All I am saying is node is an environment equal to the browser. When I install typescript. I expect it to understand my node code just the Google/Bing doesn't help because the way to install types is very On Thursday, July 14, 2016, Kitson Kelly notifications@github.com wrote:
|
User scenario Sample.js User tries to compile sample.js. S?he gets the following error: Compiler error: Typescript help site maintains a list of most used modules. Since fs is Typescript help site says:
My experience as a user is streamlined. Error messages offer contextual On Friday, July 15, 2016, Kitson Kelly notifications@github.com wrote:
|
@kitsonk @DanielRosenwasser what do you think about my list of reasons for using |
I chatted with some npm friends and dug up the original thread on peerDependencies, and you're right, it's not technically deprecated. It very much is persona non grata though, and there is risk that they will be deprecated, as that is the direction the majority of people want to go in. As such, I still think it's not a very good choice, even if it's not officially deprecated (yet). |
I definitely consider Node a platform, but platforms also often have a version associated with them. Node 10 might not have the same API surface as Node 0.10.33. I think that a good design decision is better than a convenient-right-now one. Doing "the wrong thing" with declarations for Node.js could lead to a confusing user experience. Like I mentioned in the issue, I think we are very open to the idea of working with Node and the community to improve the workflow, so I'd like to have that discussion before committing to making this a |
Yup. Sanctioned libs are the best way to go. And while I said "lib": ["node"], obviously you would want node4, node6, etc. But I get your point that any update to node could affect the declaration file. On the other hand, it would be hard to get more complicated than the current es*** picture, and almost anything is better than the current state with nearly undiscoverable access to a community driven declaration file. In the end, disoverablility of a unique procedure for |
If we force people down |
@basarat, surely it would be nice if the version could be auto-discovered, and I suppose So, yeah, if the default node configuration could just work, that would be great, but the compiler would always have to allow for specified versions...just as it is doing with es* libs, frankly. |
Any update on this? Wanted to try typescript in a Node.js environment but documentation is lacking |
All you need to get started is the ts compiler and the node types. e.g.: npm install typescript @types/node
node_modules\.bin\tsc a.ts --lib es6 #12964 tracks adding a tutorial. |
versioning applies to everything including Its just a suggestion, maybe you can came up with something better and more scalable for I think this is the case when not the best decision in terms of design should be chosen in favour of convenience and easy of use because of popularity of this issue and consumer mass that will need it. |
what is the correct target for node 7.6 for the native async / await support, but no |
It would be a |
that generates _async helpers |
For me, now that nodejs 7.7 has native async/await support, my tsconfig.json looks like this:
and generated JS does not have any _async helpers, just raw |
Had to make a guide for someone, crossposting Setting up TS for Nodetsconfig.json: {
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": **TARGET**,
...
}
...
} Install Node typings (change version to node environment): What should TARGET be?According to the compatibility chart: Node 7: es2016 |
@SimonMeskens there's no 100% compatible target. and nor would be. you can use babel preset env if you want to transpile the least possible by node version |
I think typescript should include only ECMASCRIPT apis, and put the rest in DT. |
What do you mean there's no 100% compatible target? Of course there isn't, not for browser, not for Node, not for anything else. I used the compatibility chart to figure out which targets were safe to use, just like I'd do in a browser. Target + target libs point at a spec and there is no 100% spec implementations. I also showed how to install @types/node, which is in charge of adding node-specific things (of which it seems to do a good job). I disagree entirely with the OP here, I just thought I'd share my little guide to show you how to setup TS for node, since I know people are looking in this issue for solutions. |
I appreciate your effort :) |
I don't think I've written a Node project yet that needs transpiling, since anything from es2016 onwards seems minor to me and Node 7 supports es2015 fully, but I'll keep it in mind. |
(except modules) |
TypeScript supports modules natively, so TypeScript for Node 7 does support modules. Especially with the esModuleInterop flag. |
@SimonMeskens thank you. This issue was raised in 2016, it's 3 years later and still I think your post in this thread is the only thing I found on how to get typescript targetting node. That is not very good. I'll try and send a PR against the website to include your handy guide. |
@nicferrier Please do! |
Sorry to hijack an old thread but this bit me today. Building a node app in TypeScript, buried in the codebase I had written What should have happened? The TS compiler should have shown an error to indicate that What actually happened? Unbeknown to me, the default TS settings include Needless to say, a simple mechanism to pick the node libs only would have saved me pain here! |
hope one day ts support: {
"compilerOptions": {
"lib": ["DOM", "NODE@10"] // "DOM", "ES2015", "NODE@4", "NODE@6" .....
}
} |
I understand the earlier points about the Node API surface moving too fast compared to the DOM, but the current defaults do skew heavily towards the browser. I had to go double-check the manual to be sure, but if you omit |
Hello from the future! In light of recent developments, has the team given any new thought to possibly "adopting" a first-class Node I've been trying to divorce @types/node from lib-dom but it's been an uphill climb and I think the community would benefit from some help from the team. It would be especially great if we could get a |
This is well-supported via the |
Typescript already has settings to include lib.d.ts for es6, browser, dom e.t.c. Setting up typescript with node is really painful. It seems tutorials on the web are outdated. There is tsd , typings, and supposedly some integration with npm. What is the way to go? I spent a couple of days trying to get node and typescript to work and eventually just gave up and went with javascript.
I feel typescript is alienating a huge part of js node community by adding friction to get things working. It should't be this complicated.
There is already support for moduleResolution: node, adding support for node should be a parameter away in tsconfig.
I'd say when require('fs) or import fs from 'fs' fails, VSCode editor should offer an auto-fix suggestion.
The text was updated successfully, but these errors were encountered: