typesync icon indicating copy to clipboard operation
typesync copied to clipboard

Check packages for index.d.ts without `types` or `typings` field in package.json

Open karlhorky opened this issue 4 years ago • 7 comments

#24 implemented checks for internal types in packages, but it seems like it didn't address the case of a package with an index.d.ts but no types / typings field in package.json.

This is apparently allowed, according to TypeScript docs (and also used in the wild):

https://twitter.com/sindresorhus/status/1342368373296881664

karlhorky avatar Jan 27 '21 22:01 karlhorky

Is that file included in the files array of the package?

jeffijoe avatar Jan 27 '21 22:01 jeffijoe

Yep, it is: https://unpkg.com/browse/[email protected]/package.json

Is the files key also considered?

I guess this would work in most cases! Wonder if there are any cases in which this would fail (eg. no files key, or a files key with a wildcard, etc)

karlhorky avatar Jan 28 '21 09:01 karlhorky

I suppose a check for any *.d.ts will do in the files array.

jeffijoe avatar Jan 28 '21 14:01 jeffijoe

Could you just check for the existence of index.d.ts on disk (in node_modules)? Or is there an issue with this?

karlhorky avatar Jan 28 '21 17:01 karlhorky

I would prefer not having to check node modules, currently everything works off of the package defs.

jeffijoe avatar Jan 28 '21 18:01 jeffijoe

Just so I understand the issue, you want the presence of index.d.ts in the files array to prevent the lookup of a @types/ variant, similar to the presence of a typings field?

jeffijoe avatar Feb 22 '21 13:02 jeffijoe

Yep, that seems like an accurate description 👍

karlhorky avatar Feb 22 '21 17:02 karlhorky

It seems even the NPM API doesn't return the files array either. So I think this is better solved by making packages specify their typings correctly in the package using types or typings.

jeffijoe avatar Apr 30 '23 21:04 jeffijoe

Files on disk would be an alternative.

Also, there are other ways of doing it - eg. relying on a single request (either GET or HEAD) to the popular, performant CDN UNPKG:

I think it's a popular enough pattern in npm package publishing to put in some work to try to support it.

karlhorky avatar May 01 '23 05:05 karlhorky

Files on disk would not be deterministic, as there would be a difference between running typesync before or after an npm install

Using unpkg is an idea, but considering how I just got burned from using 2 different sources, I would rather stick to what npm returns.

ky now includes the types field, by the way.

jeffijoe avatar May 01 '23 09:05 jeffijoe