From c6169d3c821c8cd113f6b716d721a390582d3ae2 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Thu, 22 Sep 2022 07:25:03 -0600 Subject: [PATCH 1/2] Amend RFC 0821: add Resolver public type --- text/0821-public-types.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/text/0821-public-types.md b/text/0821-public-types.md index 485a106023..6ff2ec6d76 100644 --- a/text/0821-public-types.md +++ b/text/0821-public-types.md @@ -37,6 +37,7 @@ Introduce public import locations for type-only imports which have previously ha - [`getOwner` and `setOwner`](#getowner-and-setowner) - [`RouteInfo`](#routeinfo) - [`RouteInfoWithAttributes`](#routeinfowithattributes) + - [`Resolver`](#resolver) - [How we teach this](#how-we-teach-this) - [`Owner`](#owner-1) - [`Transition`, `RouteInfo`, and `RouteInfoWithAttributes`](#transition-routeinfo-and-routeinfowithattributes) @@ -392,6 +393,28 @@ function takesRouteInfoWithAttributes(routeInfoWithAttributes) { ``` +### `Resolver` + +The resolver is a contract implemented by libraries outside Ember itself, such as `ember-resolver`, `ember-strict-resolver`, and any number of custom resolvers which exist in apps across the ecosystem. It has never had public documentation, but is fully public API. It is a user-constructible interface with the following definition (using the `Factory` and `FullName` types exported from the new `@ember/owner` module), available as a new named type-only export from the `@ember/engine` package: + +```ts +import EmberObject from '@ember/object'; +import type { Factory, FullName } from '@ember/owner'; + +type KnownForTypeResult = { + [fullName in `${Name}:${string}`]: boolean | undefined; +}; + +export interface Resolver extends EmberObject { + knownForType?: (type: Name) => KnownForTypeResult; + lookupDescription?: (fullName: FullName) => string; + makeToString?: (factory: Factory, fullName: FullName) => string; + normalize?: (fullName: FullName) => string; + resolve(name: string): Factory | object | undefined; +} +``` + + ## How we teach this These concepts all already exist, but need updates to and in some cases wholly new pages in Ember's API docs. From 7a3c8fa1353ee7ba3b06d0d88f282f6821618eb8 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Fri, 30 Sep 2022 16:48:31 -0600 Subject: [PATCH 2/2] Resolver and KnownForTypeResult come from '@ember/owner' --- text/0821-public-types.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/text/0821-public-types.md b/text/0821-public-types.md index 6ff2ec6d76..767d415216 100644 --- a/text/0821-public-types.md +++ b/text/0821-public-types.md @@ -395,17 +395,14 @@ function takesRouteInfoWithAttributes(routeInfoWithAttributes) { ### `Resolver` -The resolver is a contract implemented by libraries outside Ember itself, such as `ember-resolver`, `ember-strict-resolver`, and any number of custom resolvers which exist in apps across the ecosystem. It has never had public documentation, but is fully public API. It is a user-constructible interface with the following definition (using the `Factory` and `FullName` types exported from the new `@ember/owner` module), available as a new named type-only export from the `@ember/engine` package: +The resolver is a contract implemented by libraries outside Ember itself, such as `ember-resolver`, `ember-strict-resolver`, and any number of custom resolvers which exist in apps across the ecosystem. It has never had public documentation, but is fully public API. It is a user-constructible interface with the following definition (using the `Factory` and `FullName` types exported from the new `@ember/owner` module): ```ts -import EmberObject from '@ember/object'; -import type { Factory, FullName } from '@ember/owner'; - -type KnownForTypeResult = { +export type KnownForTypeResult = { [fullName in `${Name}:${string}`]: boolean | undefined; }; -export interface Resolver extends EmberObject { +export interface Resolver { knownForType?: (type: Name) => KnownForTypeResult; lookupDescription?: (fullName: FullName) => string; makeToString?: (factory: Factory, fullName: FullName) => string; @@ -414,6 +411,14 @@ export interface Resolver extends EmberObject { } ``` +The `KnownForTypeResult` utility type associated with it is also available as a named export. Unfortunately, due to currently limitations with TypeScript, you will generally have to *cast* to it, but it provides some type safety to callers, because it will *only* allow types corresponding to the passed string if users pass a string literal. + +Both are available as named, type-only, user-constructible interfaces from `@ember/owner`: + +```ts +import { Resolver, KnownForTypeResult } from '@ember/owner'; +``` + ## How we teach this