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

Leverage TypeScript 4.7 upgrade for deep type imports #2514

Closed
dummdidumm opened this issue Sep 28, 2021 · 9 comments
Closed

Leverage TypeScript 4.7 upgrade for deep type imports #2514

dummdidumm opened this issue Sep 28, 2021 · 9 comments
Labels
breaking change p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. types / typescript

Comments

@dummdidumm
Copy link
Member

dummdidumm commented Sep 28, 2021

Describe the problem

Right now we have to do ambient module hacks to define a public API through hand-written d.ts files for deep imports like @svelte/kit/hooks .

Describe the proposed solution

TypeScript 4.5 will support the exports field of a package.json along with a special import for types: microsoft/TypeScript#33079 (comment)

This means we could do

"exports": {
  "hooks": {
    "import": "./hooks.index.js",
    "types": "./types/hooks.d.ts"
  }
} 

If we do this we require people to use a compatible version of TypeScript which would be breaking change, so it would need to happen before 1.0 ideally.

Alternatives considered

  • Keep the hack as-is for now and change it in v2
  • Add a post-build-script which copies handwritten types to a location like hooks/index.d.ts where TS should pick it up

Importance

would make my life easier

Additional Information

No response

@dummdidumm dummdidumm added types / typescript p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. breaking change labels Sep 28, 2021
@dummdidumm dummdidumm added this to the 1.0 milestone Sep 28, 2021
@dummdidumm
Copy link
Member Author

Having second thoughts if this is really a 1.0 issue in the sense that it blocks a 1.0 release. If we get to releasing 1.0 before TS 4.5 is out the door and VS Code includes it in its latest version, we could do it, but even then not all users might be on the latest version of VS Code immediately, which will result in quite some friction.

@dummdidumm
Copy link
Member Author

dummdidumm commented Nov 18, 2021

TS 4.5 is out. We need to wait on Vite and ESBuild to update accordingly first and then do this and #2654 in one sweep.
Edit: Got excited too soon: TS reversed rolling out ESM support due to concerns: microsoft/TypeScript#46452 . This also affects the exports stuff.
Another edit after reading the issue: Seems they expect the ecosystem to prepare, meaning adding types entries to the package.json already, so maybe we should look into adding some kind of dual mode, supporting both.

@dummdidumm dummdidumm removed this from the 1.0 milestone Dec 13, 2021
@dummdidumm
Copy link
Member Author

Removed this from the 1.0 milestone, a backwards-compatible solution should be preferred for now. TS 4.6 at the earliest might contain stable support (which is more than 2 months from now) and even then it's not set in stone that everything will work reliably.

@benmccann benmccann changed the title Leverage TypeScript 4.5 for deep type imports Leverage TypeScript 4.6 upgrade for deep type imports Dec 15, 2021
@benmccann benmccann changed the title Leverage TypeScript 4.6 upgrade for deep type imports Leverage TypeScript 4.7 upgrade for deep type imports Mar 3, 2022
@benmccann
Copy link
Member

I sent a PR to upgrade to TypeScript 4.6: #4190. However, it looks like this feature is still only available in nightly (microsoft/TypeScript#33079 (comment)). We can check back in again for TypeScript 4.7

@Rich-Harris Rich-Harris added this to the post-1.0 milestone Apr 5, 2022
@dummdidumm
Copy link
Member Author

TS 4.7 will probably have the new resolution but we will likely not enforce it on the user (yet?). I don't know if export maps and type resolution will work for non-nodenext, too. If not, we won't be changing this.

@benmccann benmccann modified the milestones: post-1.0, 1.0 May 24, 2022
@benmccann
Copy link
Member

We're on TypeScript 4.7 and have added a types field here: #2896

I don't know if we want to add types for each sub-export? I'm changing this to 1.0 milestone since it sounds like it'd be a breaking change and there's no other blockers. Feel free to close it if it's already done

@dummdidumm
Copy link
Member Author

Not sure if we should do this in the near future. TypeScript ignores the package exports map if you don't opt in to the new module/moduleresolution (Node16 or NodeNext) - but if you do opt in to these, you are also required to adhere to the "always add explicit file endings" rules, so you need to write imports like import { foo } from './bar/index.js instead of import { foo } from './bar';

@dummdidumm dummdidumm modified the milestones: 1.0, post-1.0 Jun 18, 2022
@benmccann benmccann modified the milestones: soon, 2.0 Dec 5, 2023
@benmccann
Copy link
Member

Does this work with bundler? If so, then I think we could start doing this

@dummdidumm
Copy link
Member Author

This is obsoleted by the the dts-buddy approach of having one big ambient module file, therefore closing right away - but yes, bundler would also enable this.

@benmccann benmccann removed this from the 2.0 milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. types / typescript
Projects
None yet
Development

No branches or pull requests

3 participants