Skip to content
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

Add support for @types declaration packages #163

Closed
Tracked by #85
ChrisPearce opened this issue Aug 6, 2016 · 13 comments
Closed
Tracked by #85

Add support for @types declaration packages #163

ChrisPearce opened this issue Aug 6, 2016 · 13 comments
Milestone

Comments

@ChrisPearce
Copy link

Typescript v2 is moving to support a new approach to adding typings to a project.

Type definitions will be included in packages contained within the @types scope (e.g. @types/mypackage), which can simply be installed via npm (just like other packages).

More information can be found at:
https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/

Currently any of these @types packages that are referenced in package.json, are incorrectly recognised as unused dependencies.

@lijunle
Copy link
Member

lijunle commented Aug 7, 2016

Good feature!

Do you want to ignore all the @types packages? Or only the ones on use?

@ChrisPearce
Copy link
Author

Ideally, it would be great to recognise the ones that are used, but if that doesn't prove possible it would be great to have an option that just ignores all @types scoped packaged.

@ChrisPearce
Copy link
Author

As a workaround, I'm currently using the following command to ignore all @types scoped packages:
depcheck --ignores @types/*

@GongT
Copy link

GongT commented Aug 31, 2018

In a typescript project, @types packages can be "used" with 3 different ways:

2 way in source code, that's simple to parse:

///<reference types="node"/>
import React from 'react';
import someThing from '@scoped/package'; // <- "@types/scoped__package"

another in tsconfig.json, but is complicated.
tsconfig.json document
( see section @types, typeRoots and types)

for each tsconfig found in project:

  1. can be renamed to everything. it's just a JSON, no field is required. (And also can add extra fields you want)
  2. can extend another one. every file on the chain can use as the final one.
  3. once a tsconfig file result contains typeRoots, but "*bla/bla/*node_modules/@types" not included, then this file is nothing to do with @types.
  4. if there is no typeRoots nor types, all @types package can mark as used. no other detection is needed in entire project.
  5. if there is types field, only listed package used by this file.

("typeRoots" and "types" is sub field of "compilerOptions")

@GongT
Copy link

GongT commented Aug 31, 2018

BTW, @types package should not appear in dependencies, but should always in devDependencies.
except some extreme rear situation.

@GongT
Copy link

GongT commented Aug 31, 2018

Oh, When I start to write some code, I found that, the first 2 ways is also affected by tsconfig.json.
It's really hard to do a "complete" check....
I think this big job worth to create a new tool...

@mnkhouri
Copy link
Member

mnkhouri commented Sep 1, 2018

@GongT thanks for the research, that does make it seem quite complex to properly detect @types usages when it is modified by tsconfig.json.

Instead of trying to analyze it ourselves, I wonder if there might be some way to extract this information from the typescript compiler? I haven't done a lot of research, but some documentation is here.

@GongT
Copy link

GongT commented Sep 1, 2018

I have not look into depcheck's parser, current parseTypescript not load user's tsconfig.json, this need first update.

ts.transpile cannot used to analyze a "program", types option is hard code to undefined.

here is a simple script to list all files used when compile a project:
https://gist.github.com/GongT/1d5113a2ffd8ac7cac53123af61039d1

@conartist6
Copy link
Contributor

I've opened a PR that would fix this. If people here could chip in a review perhaps it would speed the process of getting it shipped.

conartist6 added a commit to conartist6/depcheck that referenced this issue Sep 20, 2019
conartist6 added a commit to conartist6/depcheck that referenced this issue Sep 20, 2019
conartist6 added a commit to conartist6/depcheck that referenced this issue Sep 20, 2019
conartist6 added a commit to conartist6/depcheck that referenced this issue Sep 20, 2019
conartist6 added a commit to conartist6/depcheck that referenced this issue Sep 20, 2019
conartist6 added a commit to conartist6/depcheck that referenced this issue Sep 20, 2019
conartist6 added a commit to conartist6/depcheck that referenced this issue Oct 21, 2019
@rumpl rumpl closed this as completed in f08f213 Oct 27, 2019
@ljharb
Copy link

ljharb commented Mar 1, 2024

This still doesn't seem solved - i'm still getting unused warnings for DT packages.

@SiRocke
Copy link

SiRocke commented Mar 29, 2024

I've also made it to this point. Doing some major cleanup and it looks to still be an issue.

@navignaw
Copy link

navignaw commented May 24, 2024

I'm seeing an issue as well. My best guess is that the solution works for importing types directly, but does not work for the /// <reference types="foo" /> syntax.

Here's a reproducible example:

/// <reference types="w3c-web-usb" />

const supportsUsb = !!navigator?.usb

with this, depcheck reports

Unused devDependencies
* @types/w3c-web-usb

however, this works successfully with no error:

import type { default as foo } from 'w3c-web-usb'

const supportsUsb = !!navigator?.usb

@ljharb
Copy link

ljharb commented May 24, 2024

The point of DT types is that you import them from the non-DT package name.

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

No branches or pull requests

8 participants