GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use dtsGen to generate declaration files for imported modules with no declarations · Issue #25746 · microsoft/TypeScript

Use dtsGen to generate declaration files for imported modules with no declarations #25746

New issue
Copy link
New issue
Copy link
Closed
#26588
Closed
Use dtsGen to generate declaration files for imported modules with no declarations#25746
#26588
Copy link
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueDomain: LS: Quick FixesEditor-provided fixes, often called code actions.Editor-provided fixes, often called code actions.FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
TypeScript 3.2

Description

@dannycochran
Danny Cochran (dannycochran)
opened on Jul 17, 2018
Issue body actions

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 with noImplicitAny: 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 use dts-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 put index.d.ts files inside of libraries within your node_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 under compilerOptions could do this?

Use Cases

Described above

Examples

Described above

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
Reacted by Kris Kaczor, Justin Grant, Fabian Morón Zirfas, Bruno Fantauzzi, Lucio Tarantino, uid11 and Andrii Oriekhov

Activity

Next
  1. mhegazy commented on Jul 18, 2018

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Jul 18, 2018
    Contributor
    More actions

    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.

    Reacted by Steven, Stronger, ExE Boss and Fabian Morón Zirfas
  2. Mohamed Hegazy (mhegazy)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    Domain: LS: Quick FixesEditor-provided fixes, often called code actions.Editor-provided fixes, often called code actions.
    on Jul 18, 2018
  3. Mohamed Hegazy (mhegazy)
    changed the title [-]Add "dtsGenFallback" to compilerOptions[/-] [+]Use dtsGen to generate declaration files for imported modules with no declarations[/+] on Jul 18, 2018
  4. Mohamed Hegazy (mhegazy)
    assigned
    ghost
    on Jul 18, 2018
  5. Mohamed Hegazy (mhegazy)
    added this to the TypeScript 3.1 milestone on Jul 18, 2018
  6. dannycochran commented on Jul 18, 2018

    @dannycochran
    Danny Cochran (dannycochran)
    on Jul 18, 2018
    Author
    More actions

    Mohamed Hegazy (@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?

  7. mhegazy commented on Jul 18, 2018

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Jul 18, 2018
    Contributor
    More actions

    I think the only thing you'd need to check for on build is if the package updated,

    that is a general problem that we have today as well with the quick fix to install @types packages. Today we install the latest @types package regardless. ideally we would be more cognizant of the version and install the right version. Then the next problem is detecting version mismatches and warning the user about them, giving them the option to update. I think this feature would follow the same pattern.

    how would you imagine users activating this feature?

    If a module has no declaration file in it, no matching @types package in the registry, and no ambient module declaration, the user will get a Suggestion diagnostic (three little dots under the name in VSCode), along with a light pulp to create the declaration file for the missing package.

    Reacted by Pedro Barbiero and Amin Pakseresht
  8. dannycochran commented on Jul 18, 2018

    @dannycochran
    Danny Cochran (dannycochran)
    on Jul 18, 2018
    Author
    More actions

    If a module has no declaration file in it, no matching Types (@types) package in the registry, and no ambient module declaration, the user will get a Suggestion diagnostic (three little dots under the name in VSCode), along with a light pulp to create the declaration file for the missing package.

    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 any typing).

    Where would the declaration file go? I suppose you could put it into node_modules/<untypedPackage>/index.d.ts, but then it doesn't get checked in.

    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 compilerOptions.typeRoots and look for a local typings folder?

    Separately, if a user has no local typings folder, you'd have to create one for them and modify their compilerOptions.typeRoots. Not sure the language service can do that?

  9. mhegazy commented on Jul 18, 2018

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Jul 18, 2018
    Contributor
    More actions

    Where would the declaration file go? I suppose you could put it into node_modules//index.d.ts, but then it doesn't get checked in.

    node_modules is not the best place. since that is not checked in your project. we will have to pick a name for a folder for these packages. I think we can use typeRoots as a way of inferring the location of types.

  10. Ryan Cavanaugh (RyanCavanaugh)
    added
    CommittedThe team has roadmapped this issueThe team has roadmapped this issue
    on Aug 15, 2018
  11. ghost added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    on Sep 4, 2018
  12. ghost closed this as completedin #26588on Sep 18, 2018
  13. dannycochran commented on Sep 28, 2018

    @dannycochran
    Danny Cochran (dannycochran)
    on Sep 28, 2018
    Author
    More actions

    Andy (Andrewkraft) (@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.

  14. 8 remaining items

  15. dannycochran commented on Nov 9, 2018

    @dannycochran
    Danny Cochran (dannycochran)
    on Nov 9, 2018
    Author
    More actions

    Ran npm install typescript@next --save-dev (resolved to 3.2.0-dev.20181107), and pointed VSCode to use the local install from node_modules. Looks like I'm getting that same error (Expected isNewFile):

    https://gist.github.com/dannycochran/87622db7624d9cadd41caa0dfa9f8d24

  16. Daniel Rosenwasser (DanielRosenwasser)
    modified the milestones: TypeScript 3.1, TypeScript 3.2 on Nov 9, 2018
  17. arogg commented on Nov 10, 2018

    @arogg
    arogg
    on Nov 10, 2018
    More actions

    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.
    No one wants to clone the DT repo, read a bunch of documentation and create pull requests. I mean it has to happen, but the user does not want to associate with additional work, they want it to be easy and "fun". Also I want to use any typings I edit directly, without waiting for the pull request to get through.
    I think because such a tooling does not exist, a lot of people don't bother contributing to DT and just create their typings locally (me too).
    This hurts TypeScript MASSIVELY I think. But because you can't measure the detrimental effect, it goes completely unnoticed.
    I want to make a feature request surrounding this because I am sure something is missing here, but I am uncertain how I would go about this, especially since I don't really have any idea on how to implement something like this. I just know that it would be better if not everyone was just creating typings for themselves (or using "any").
    Anyone with me on this?

    Reacted by flex-io-glitch, Stronger and Spencer Park
  18. arogg commented on Nov 10, 2018

    @arogg
    arogg
    on Nov 10, 2018
    More actions

    a way to quickly augment typings from the DT repository would be more useful

    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 (@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 :)

    Reacted by Bart Langelaan, Marco Q, Alex, Tobias Cudnik, Jakub Navrátil, Stronger, Nathan Shively-Sanders, Ádám Lippai, Mathieu M-Gosselin, Aleksey Levenstein and 2 more
    Reacted by Bart Langelaan, Tomáš Szabo, Stronger, Wes Roberts and Daniel Garcia
  19. dannycochran commented on Nov 10, 2018

    @dannycochran
    Danny Cochran (dannycochran)
    on Nov 10, 2018
    Author
    More actions

    I want to make a feature request surrounding this because I am sure something is missing here, but I am uncertain how I would go about this

    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.

  20. wclr commented on Nov 30, 2018

    @wclr
    Alex (wclr)
    on Nov 30, 2018
    More actions

    Currently contributing to DT is a crappy endeavor.

  21. dannycochran commented on Dec 1, 2018

    @dannycochran
    Danny Cochran (dannycochran)
    on Dec 1, 2018
    Author
    More actions

    Andy (Andrewkraft) (@Andy-MS) did you get a chance to look at the logs I posted here following your previous comment?

    #25746 (comment)

  22. sandersn commented on Dec 14, 2018

    @sandersn
    Nathan Shively-Sanders (sandersn)
    on Dec 14, 2018
    Member
    More actions

    Danny Cochran (@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.

  23. aminpaks commented on Apr 1, 2019

    @aminpaks
    Amin Pakseresht (aminpaks)
    on Apr 1, 2019
    Contributor
    More actions

    Danny Cochran (@dannycochran) I cloned your repo and it appears there is a problem with folder structure. TS is creating the type definition at the root/types/foo.d.ts if the folder exists. Also it adds a path redirection to tsconfig so the declaration can be found.

    I don't think this is ideal, the better choice would be taking advantage of typeRoots.

    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;
    };
  24. Amin Pakseresht (aminpaks)
    unassigned on Apr 1, 2019
  25. borekb commented on May 20, 2019

    @borekb
    Borek Bernard (borekb)
    on May 20, 2019
    More actions

    Could someone clarify the status of this please? Sounds very useful but I'm confused by the combination of these things:

    • This has been marked as fixed and successfully closed last September: Use dtsGen to generate declaration files for imported modules with no declarations #25746 (comment)
    • It's still on the roadmap, and has actually been de-prioritized – I think it used to be scheduled for 3.3 or 3.4, now it's in the Future category.

    Thanks.

  26. dannycochran commented on Jun 4, 2019

    @dannycochran
    Danny Cochran (dannycochran)
    on Jun 4, 2019
    Author
    More actions

    Amin Pakseresht (@aminpaks) Borek Bernard (@borekb)

    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 tsconfig.json setup. I have multiple TS projects with varying tsconfigs, and in none of them is this feature showing up. So, this will never really be discoverable.

    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.

    Reacted by Amin Pakseresht and Kevin Heye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueThe team has roadmapped this issueDomain: LS: Quick FixesEditor-provided fixes, often called code actions.Editor-provided fixes, often called code actions.FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      • TypeScript 3.2

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.