-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX stable] Fix types for Resolver contract
We need `Resolver` implementors (e.g. `ember-resolver`) to be able to provide a class which can satisfy this contract, which means we need to support either a static `.create()` method or a constructor (with a strong preference for the constructor). This is likely to be a common issue with this kind of assignability of classic classes, because it ultimately comes down to the definition of `Factory` and `CoreObject`. To resolve this, add two overloads for `CoreObject.prototype.create`: - One which just produces an instance type with no args at all. - One which produces an instance type by merging with passed-in args. These two overloads also improve the type safety of `.create()` more generally, allowing us to handle a couple cases which the types did not previously account for (and indeed had given up handling). This also adds a smoke test for something shaped like the `app.ts` file generated for every Ember app, so that we can be sure that the code we emit when generating an app actually works. For now, this simply copies over the types from `ember-resolver`, `ember-load-initializers`, and a basic `environment`. (cherry picked from commit 4c99fa1)
- Loading branch information
1 parent
fdc1283
commit 9939768
Showing
11 changed files
with
71 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,8 @@ | ||
import { expectTypeOf } from 'expect-type'; | ||
import { Person } from './create'; | ||
|
||
// Let's see some *real* type-unsafety! These demonstrate that we just | ||
// absolutely lie about the runtime behavior for `.create()`. 😬 The result of | ||
// calling these with `firstName: 99` is to *change the type* of the resulting | ||
// object from `Person` to `Person` with `firstName: number`. While we might | ||
// *like* to prevent that call and make sure that `firstName` matches the type | ||
// we cannot reasonably do so. | ||
|
||
expectTypeOf(Person.create({ firstName: 99 })).toEqualTypeOf< | ||
Person & { | ||
firstName: number; | ||
} | ||
>(); | ||
expectTypeOf(Person.create({}, { firstName: 99 })).toEqualTypeOf< | ||
Person & { | ||
firstName: number; | ||
} | ||
>(); | ||
expectTypeOf(Person.create({}, {}, { firstName: 99 })).toEqualTypeOf< | ||
Person & { | ||
firstName: number; | ||
} | ||
>(); | ||
// @ts-expect-error | ||
Person.create({ firstName: 99 }); | ||
// @ts-expect-error | ||
Person.create({}, { firstName: 99 }); | ||
// @ts-expect-error | ||
Person.create({}, {}, { firstName: 99 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,8 @@ | ||
import { expectTypeOf } from 'expect-type'; | ||
import { Person } from './create'; | ||
|
||
// Let's see some *real* type-unsafety! These demonstrate that we just | ||
// absolutely lie about the runtime behavior for `.create()`. 😬 The result of | ||
// calling these with `firstName: 99` is to *change the type* of the resulting | ||
// object from `Person` to `Person` with `firstName: number`. While we might | ||
// *like* to prevent that call and make sure that `firstName` matches the type | ||
// we cannot reasonably do so. | ||
|
||
expectTypeOf(Person.create({ firstName: 99 })).toEqualTypeOf< | ||
Person & { | ||
firstName: number; | ||
} | ||
>(); | ||
expectTypeOf(Person.create({}, { firstName: 99 })).toEqualTypeOf< | ||
Person & { | ||
firstName: number; | ||
} | ||
>(); | ||
expectTypeOf(Person.create({}, {}, { firstName: 99 })).toEqualTypeOf< | ||
Person & { | ||
firstName: number; | ||
} | ||
>(); | ||
// @ts-expect-error | ||
Person.create({ firstName: 99 }); | ||
// @ts-expect-error | ||
Person.create({}, { firstName: 99 }); | ||
// @ts-expect-error | ||
Person.create({}, {}, { firstName: 99 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Application from '@ember/application'; | ||
import Resolver from './ember-resolver-alike'; | ||
import loadInitializers from './ember-load-initializers-alike'; | ||
import config from './environment-alike'; | ||
|
||
export default class App extends Application { | ||
modulePrefix = config.modulePrefix; | ||
podModulePrefix = config.podModulePrefix; | ||
Resolver = Resolver; | ||
} | ||
|
||
loadInitializers(App, config.modulePrefix); |
10 changes: 10 additions & 0 deletions
10
type-tests/smoke/basic-app-alike/ember-load-initializers-alike.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
declare global { | ||
var requirejs: { | ||
_eak_seen: Object; | ||
}; | ||
} | ||
import Engine from '@ember/engine'; | ||
/** | ||
* Configure your application as it boots | ||
*/ | ||
export default function loadInitializers(app: typeof Engine, prefix: string): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Resolver as ResolverContract } from '@ember/owner'; | ||
import EmberObject from '@ember/object'; | ||
export default class Resolver extends EmberObject {} | ||
export default interface Resolver extends Required<ResolverContract> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Type declarations for | ||
* import config from 'my-app/config/environment' | ||
*/ | ||
declare const config: { | ||
environment: string; | ||
modulePrefix: string; | ||
podModulePrefix: string; | ||
locationType: 'history' | 'hash' | 'none' | 'auto'; | ||
rootURL: string; | ||
APP: Record<string, unknown>; | ||
}; | ||
|
||
export default config; |