-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Use dtsGen to generate declaration files for imported modules with no declarations #25746
Comments
I do not think the compiler is the right place for this feature. the language service, however, is. This is a feature that you do not do on every build, nor should it. it is rather one that you do once when you first import a package. |
@mhegazy thank you for the quick response and triaging! That makes sense that it would be under the language service. I think the only thing you'd need to check for on build is if the package updated, as an updated package may now have typings associated with it, or the output from dts-gen could be different. To clarify though, how would you imagine users activating this feature? |
that is a general problem that we have today as well with the quick fix to install
If a module has no declaration file in it, no matching |
Gotcha. I like that as it forces the user to understand what's happening, and that the typings are going to be very lax (dts-gen tends to do a lot of Where would the declaration file go? I suppose you could put it into If you put it into the app directory, you'd probably want to check if there's already a local typings folder for untyped packages or for augmented third party typings. Determining where that existing folder lives is probably non-obvious.. I suppose you could scan Separately, if a user has no local typings folder, you'd have to create one for them and modify their |
|
@andy-ms thanks for working on this! I noticed you marked the issue as fixed, but in the PR it says the functionality is not yet exposed to consumers? I just tried testing in 3.1 and didn't see a code fix lightbulb. |
@dannycochran It's working for me in Make sure you have your editor set up to use the latest TypeScript install. (vscode instructions) |
hmm @andy-ms I tried that example locally and it does not work for me still. here's an example repo, if you run: npm i && npm run build you'll see the error. |
Hi @dannycochran, this feature doesn't affect compilation. But if you open |
@dannycochran In that image, it looks like 'foo' is at the root, rather than inside |
@andy-ms it's checked into the root, but it gets copied to are you able to clone the repo I showed above and see the correct code fix after running |
It works for me when I run |
@andy-ms here's the https://gist.github.com/dannycochran/b130c88c3a588dee727daac2e6c0f600 |
Ran https://gist.github.com/dannycochran/87622db7624d9cadd41caa0dfa9f8d24 |
I think a way to quickly augment typings from the DT repository would be more useful for the growth of TypeScript. I imagine just editing the typings from DT where all stuff surrounding pull requests and tests is abstracted away from the user and the user is guided through it automatically. |
We already have this. You can, for example, augment any module with new exports, or add new members to its interfaces. https://www.typescriptlang.org/docs/handbook/declaration-merging.html |
There is a misunderstanding here. I'm talking about augmenting the DT repository, not just their types. (And in some cases declarations cannot be merged, btw.) Just augmenting the DT types for personal use is OK, but only good in the short term. It does not benefit the community and you now must share that augmented typing between all your projects that use that package. More specifically, I'm talking about the process of cloning the DT repo, reading the DT docs, augmenting the typings, maybe creating the missing type, adding the tests, pushing and creating the pull request and waiting until the PR is approved, so that you may then replace your manually copied/augmented DT typing with the new official @types packages. The last part is the most annoying for me but actually the whole process should be thought about. I want that process highly integrated and streamlined. I want to go to that DT type from within VS code and edit it, augment the DT tests and everything else is taken care of. This is just a rough idea of how it should be. I have not thought this out. But I think solutions will present themselves. You want people to prefer adding to DT to just locally creating types for themselves. So you need to make the process fast and enjoyable. I also think this is something TypeScript can still benefit lots from. I very VERY often see inexact DT typings. It is not uncommon. I think this warrants additional attention for the issue. Getting people to the point that they prefer adding to the DT repository, rather than creating local typings, should be the goal. I am sure DT is not tapping it's full potential. LOTS (and lots) of people still prefer just creating typings for themselves. Naturally, statistics for this do not exist, but I think it's self-explainatory :) |
You can create a new issue with a feature request template: https://github.com/Microsoft/TypeScript/issues/new?template=Feature_request.md In general, what you're describing could be built by the TS team, or it could be built by a member of the TS community. I think it's a good idea, and it sounds like you're unsure about where to start. I'd suggest by starting a dedicated issue for the conversation, or you could join the TypeScript gitter channel and try vetting the idea there. I'm not sure this issue is the right place to vet your idea, though. This feature is landed and merged to master, and in no way precludes the feature you're describing. I believe they would be complimentary. |
Currently contributing to DT is a crappy endeavor. |
@andy-ms did you get a chance to look at the logs I posted here following your previous comment? |
@dannycochran Andy moved to the .NET team so now I'm taking a look at this codefix to see why it doesn't work. I'll take a look at your latest logs after I work through the earlier repros in this thread. |
@dannycochran I cloned your repo and it appears there is a problem with folder structure. TS is creating the type definition at the I don't think this is ideal, the better choice would be taking advantage of Here is the changes VSCode/TS offered: // tsconfig.json {
"compilerOptions": {
"baseUrl": ".",
"paths": { "*": ["types/*"] },
"module": "commonjs",
"strict": true,
"outDir": "build"
},
"include": ["src"]
} // types/foo.d.ts export const x: number;
export const y: {
bar: string;
foo: string;
}; |
Could someone clarify the status of this please? Sounds very useful but I'm confused by the combination of these things:
Thanks. |
As far as I can tell, this feature doesn't really work at the moment. I don't think it should only work under a specific I still think this is a big issue for newer TS developers (and experienced devs), so I think it's worth revisiting to make this more seamless. |
Search Terms
dts-gen
third party modules
fallback
typings
modules without typings
Suggestion
One of the biggest concerns I've heard from new users using TypeScript is dealing with libraries that have no typings provided (neither in the library itself nor in DefinitelyTyped). In a
strict
TS environment or withnoImplicitAny: true
, TypeScript will complain about a library you import that has no associated typings definition.Your options at this point are to provide your own typings file in a local
typings
folder, or usedts-gen
to generate one for you.Personally, I find this to be a useful exercise but newer users get tripped up on it and frequently cite it as one of the harder parts about TS adoption.
I had considered creating a library (similar to https://github.com/tamayika/types-local) that would use
dts-gen
to automatically putindex.d.ts
files inside of libraries within yournode_modules
folder that have no associated typings, provided you've imported the library in your code. This would probably end up being gross and an npm anti-pattern.Instead, it would be nice if the compiler could do this for us. If you have imported code w/o typings, the TS compiler could use
dts-gen
to generate typings (which obviously are imperfect) which would allow folks to proceed without worrying immediately about whether the library has typings.Maybe a flag called
dtsGenFallback
undercompilerOptions
could do this?Use Cases
Described above
Examples
Described above
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: