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

feat: Add types for default export #127

Merged
merged 2 commits into from
Mar 2, 2024
Merged

Conversation

FoxxMD
Copy link
Contributor

@FoxxMD FoxxMD commented Feb 23, 2024

Closes #126

@FoxxMD
Copy link
Contributor Author

FoxxMD commented Feb 23, 2024

I ran npm run prepublish and then linked the local repo into my own project withnpm install ../with-local-tmpdir and the types seemed to work. I'm not familiar with @dword-design/base or your publish process (it looks pretty involved) so I don't know if I missed anything.

@dword-design
Copy link
Owner

@FoxxMD lgtm. Can you move it to root folder and rename it to types.d.ts?

@FoxxMD
Copy link
Contributor Author

FoxxMD commented Feb 28, 2024

Are sure that's where it should be? When i run prepare and prepublishOnly it adds types.d.ts to the package.json files but does not copy the file to dist

image

I don't think that will publish correctly

@dword-design
Copy link
Owner

@FoxxMD I will be published like this. It's part of the files array in package.json.
Bildschirmfoto 2024-02-28 um 18 47 38

Is it not a best practice to do it like this?

@FoxxMD
Copy link
Contributor Author

FoxxMD commented Feb 28, 2024

My understanding is that types.d.ts is a common pattern for naming a types entrypoint but its not magic. The types for each file either need the same name as the file the declarations work on (index.js -> index.d.ts) and/or use types property in package.json to specify the entrypoint IE

{
 "name": "with-local-tmp-dir",
 ...
 "main": "dist/index.js",
 "types": "types.d.ts"
}

@dword-design
Copy link
Owner

@FoxxMD Ok but then having types.d.ts in root is fine, right?

@FoxxMD
Copy link
Contributor Author

FoxxMD commented Feb 29, 2024

I finally tracked down why this was causing issues:

https://stackoverflow.com/a/76212193/1469797

The types field in package.json is no longer used if there is an exports field

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing

With TypeScript’s original Node support, it would look for a "main" field, and then look for declaration files that corresponded to that entry. For example, if "main" pointed to ./lib/index.js, TypeScript would look for a file called ./lib/index.d.ts. A package author could override this by specifying a separate field called "types" (e.g. "types": "./types/index.d.ts").

The new support works similarly with import conditions. By default, TypeScript overlays the same rules with import conditions - if you write an import from an ES module, it will look up the import field, and from a CommonJS module, it will look at the require field. If it finds them, it will look for a corresponding declaration file. If you need to point to a different location for your type declarations, you can add a "types" import condition.

If you really really want to use types.d.ts at root it can be done but package.json needs to be updated with an exports structure that specifies types separately from default as shown in the TS docs example.

Or...keep index.d.ts where it is:

  • which is what TS is expecting anyways
  • keeps the type definitions close, structurally, to the file it types
  • doesn't required altering package.json at all

@dword-design dword-design merged commit e8c9bc8 into dword-design:master Mar 2, 2024
1 check passed
@dword-design
Copy link
Owner

@FoxxMD Ok I had a look at it and I'm fine with that :). Thanks for investigating!

github-actions bot pushed a commit that referenced this pull request Mar 2, 2024
# [5.1.0](v5.0.1...v5.1.0) (2024-03-02)

### Features

* Add types for default export ([#127](#127)) ([e8c9bc8](e8c9bc8))
Copy link

github-actions bot commented Mar 2, 2024

🎉 This PR is included in version 5.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

feat: Add typescript types
2 participants