From 84d936db3f1edc936d8626db639174dc36e01d3b Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 26 Feb 2019 14:38:40 -0800 Subject: [PATCH] Ember workarounds and fixes for TS 3.4 1. Ember-data tests had the wrong return type in a couple of places. TS 3.4 catches this error, and this PR fixes it. 2. Ember-data's Model class became invariant in 3.4 because of its use of the `this` type. This PR uses a `this` parameter with a method-local type parameter to avoid using Model's `this` type. 3. Ember v2's tests were broken by TS 3.4's more complete return-type inference, which caused a variable declaration's type annotation to break inference through contextual typing. This PR converts the type annotation to type parameters on the call, which short-circuits inference entirely. It's also shorter. --- types/ember-data/index.d.ts | 5 +++-- types/ember-data/test/relationships.ts | 2 +- types/ember-data/test/store.ts | 5 +++-- types/ember-data/v2/index.d.ts | 2 +- types/ember-data/v2/test/relationships.ts | 2 +- types/ember-data/v2/test/store.ts | 5 +++-- types/ember/v2/test/mixin.ts | 2 +- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/types/ember-data/index.d.ts b/types/ember-data/index.d.ts index 601a45faf3..6cb514556c 100644 --- a/types/ember-data/index.d.ts +++ b/types/ember-data/index.d.ts @@ -543,8 +543,9 @@ export namespace DS { * invoking the callback with the name of each relationship and its relationship * descriptor. */ - eachRelationship( - callback: (name: string, details: RelationshipMeta) => void, + eachRelationship( + this: T, + callback: (name: string, details: RelationshipMeta) => void, binding?: any ): any; /** diff --git a/types/ember-data/test/relationships.ts b/types/ember-data/test/relationships.ts index dee9519b34..13a526ba6a 100644 --- a/types/ember-data/test/relationships.ts +++ b/types/ember-data/test/relationships.ts @@ -26,7 +26,7 @@ p.eachRelationship((n, meta) => { let m: 'belongsTo' | 'hasMany' = meta.kind; }); -class Comment extends DS.Model { +export class Comment extends DS.Model { author = DS.attr('string'); } diff --git a/types/ember-data/test/store.ts b/types/ember-data/test/store.ts index 9c1194c711..b6bb25993c 100644 --- a/types/ember-data/test/store.ts +++ b/types/ember-data/test/store.ts @@ -1,6 +1,7 @@ import Ember from 'ember'; import DS from 'ember-data'; import { assertType } from './lib/assert'; +import { Comment } from './relationships'; declare const store: DS.Store; @@ -119,7 +120,7 @@ const MyRouteAsync = Ember.Route.extend({ const store = this.get('store'); return await store.findRecord('post-comment', 1); }, - async afterModel(): Promise> { + async afterModel(): Promise> { const post = await this.get('store').findRecord('post', 1); return await post.get('comments'); } @@ -132,7 +133,7 @@ class MyRouteAsyncES6 extends Ember.Route { async model(): Promise { return await this.store.findRecord('post-comment', 1); } - async afterModel(): Promise> { + async afterModel(): Promise> { const post = await this.store.findRecord('post', 1); return await post.get('comments'); } diff --git a/types/ember-data/v2/index.d.ts b/types/ember-data/v2/index.d.ts index 3afd091ffe..e6dd65eb3b 100644 --- a/types/ember-data/v2/index.d.ts +++ b/types/ember-data/v2/index.d.ts @@ -525,7 +525,7 @@ export namespace DS { * invoking the callback with the name of each relationship and its relationship * descriptor. */ - eachRelationship(callback: (name: string, details: RelationshipMeta) => void, binding?: any): any; + eachRelationship(this: T, callback: (name: string, details: RelationshipMeta) => void, binding?: any): any; /** * Represents the model's class name as a string. This can be used to look up the model's class name through * `DS.Store`'s modelFor method. diff --git a/types/ember-data/v2/test/relationships.ts b/types/ember-data/v2/test/relationships.ts index 06795c0e26..21e66c6be5 100644 --- a/types/ember-data/v2/test/relationships.ts +++ b/types/ember-data/v2/test/relationships.ts @@ -26,7 +26,7 @@ p.eachRelationship((n, meta) => { let m: 'belongsTo' | 'hasMany' = meta.kind; }); -class Comment extends DS.Model { +export class Comment extends DS.Model { author = DS.attr('string'); } diff --git a/types/ember-data/v2/test/store.ts b/types/ember-data/v2/test/store.ts index 2584e7bdf3..902ea99852 100644 --- a/types/ember-data/v2/test/store.ts +++ b/types/ember-data/v2/test/store.ts @@ -1,6 +1,7 @@ import Ember from 'ember'; import DS from 'ember-data'; import { assertType } from './lib/assert'; +import { Comment } from './relationships'; declare const store: DS.Store; @@ -119,7 +120,7 @@ const MyRouteAsync = Ember.Route.extend({ const store = this.get('store'); return await store.findRecord('post-comment', 1); }, - async afterModel(): Promise> { + async afterModel(): Promise> { const post = await this.get('store').findRecord('post', 1); return await post.get('comments'); } @@ -132,7 +133,7 @@ class MyRouteAsyncES6 extends Ember.Route { async model(): Promise { return await this.store.findRecord('post-comment', 1); } - async afterModel(): Promise> { + async afterModel(): Promise> { const post = await this.store.findRecord('post', 1); return await post.get('comments'); } diff --git a/types/ember/v2/test/mixin.ts b/types/ember/v2/test/mixin.ts index a89f490a11..46566a9df9 100755 --- a/types/ember/v2/test/mixin.ts +++ b/types/ember/v2/test/mixin.ts @@ -6,7 +6,7 @@ interface EditableMixin { isEditing: boolean; } -const EditableMixin: Ember.Mixin = Ember.Mixin.create({ +const EditableMixin = Ember.Mixin.create({ edit() { this.get('controller'); console.log('starting to edit');