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

[FEATURE] Stable types for @ember/owner #20288

Merged
merged 7 commits into from
Nov 30, 2022
Merged

Conversation

chriskrycho
Copy link
Contributor

@chriskrycho chriskrycho commented Nov 29, 2022

This PR introduces the first stable types published from Ember's source code! 🎉 🚀

Here's how it works:

  1. Using the infrastructure introduced in add type tests infrastructure for supported TS versions #20185 (with some bug fixes), we include references to the types from @ember/owner and its dependency @ember/-internals/owner in the types/stable/index.d.ts file we generate as part of the build (3ca9834).

  2. Accordingly, we now treat generating these types as part of the build operation: yarn build both runs a normal Ember build to generate the JS (build:js) and runs types/publish.mjs to emit these type definitions.

  3. Since those types now exist, we also remove the corresponding types from types/preview, so that end users who are using our recommended imports—

    import 'ember-source/types';
    import 'ember-source/types/preview';

    —will not get type conflicts from our own types (also 3ca9834).

  4. We collapse the two sets of type tests together (74c2895). This was just a miss before: I didn't realize that we were going to necessarily want to be testing them as a coherent whole, but of course, that's how our consumers will be experiencing them, so that's how we need to test them. The combination of (1) and (3) made this really obvious.

As part of this PR, I introduced an absolute nightmare/monstrosity in abusing regex find and replace for the types/publish.mjs script to get rid of declare usages in the emitted module (see 86f6e52). In a future PR—indeed, the next PR, before landing any more such changes—I will switch that over to using recast to remove those in a more reasonable way, and then running prettier against the results so that the emitted type definition files look reasonable.

- Rename the existing `build` command to `build:js`, introduce a
  `build:types` command, and make a new top-level `build` command that
  dispatches both of the others with `npm-run-all`.

- Update handling for type checking to include separate passes for the
  library TS code and *each* of the type test packages.
We might want *some* lints on these at some point, but for now this is
the right tradeoff here.
- The modules needed to be "absolute" not "relative" for the filtering
  to work correctly.
- That in turn meant we needed to *insert* a relative lookup.
- But we also needed to make sure we treat top-level packages *as such*
  rather than linking to their `index`, else TS does not resolve them
  correctly with these side effect modules.
@chriskrycho chriskrycho added the TypeScript Work on Ember’s types label Nov 29, 2022
- Remove `@ember/-internals/owner` and `@ember/owner` from the list of
  excluded preview types in the types publishing script, so they now
  get emitted correctly into `types/stable`.

- Remove `@ember/owner` from the preview types and put it in the stable
  types instead, so users don't get conflicting type dependencies.

- Internally in `@ember/owner`, use absolute package paths, not
  relative. For referencing other (even internal) packages, it's
  important that we *not* use relative paths, so that (a) the published
  types work when wrapped in `declare module` statements but also (b)
  we have clearer boundaries for them, which will unlock further
  improvements to this infrastructure in the future.
These really belong in the `@ember/application` re-export, not in the
tests for `@ember/owner` itself, which should have no knowledge of the
existence of `@ember/application` (but vice versa is fine).
This guarantees that we will be testing exactly what our consumers use,
with the only difference being the specific import paths used to expose
the side effect modules. It also keeps the type tests from diverging
between the two sets of types.

Previously, following the DefinitelyTyped approach, all the preview
type modules had their own `tsconfig.json`, but this actually made them
fail to interoperate with stable types, and this approach *only* works
if they work together. Removing those means they operate as a single
coherent set of type definitions with the stable types, distinguished
*only* by the way they are authored.
Copy link
Contributor

@dfreeman dfreeman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High level this all makes sense to me! Were all the types/preview/**/tsconfig.json files unneeded to begin with or did moving the tests around somehow impact that?

@chriskrycho
Copy link
Contributor Author

Were all the types/preview/**/tsconfig.json files unneeded to begin with or did moving the tests around somehow impact that?

I believe they were unneeded in the first place, they were just leftover cruft from the DefinitelyTyped migration: DefinitelyTyped does need them given how its structure works, but we don't because we are doing the declare module shenanigans.

@chriskrycho chriskrycho merged commit d7b3f9d into master Nov 30, 2022
@chriskrycho chriskrycho deleted the stabilize-owner-types branch November 30, 2022 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TypeScript Work on Ember’s types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants