-
-
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.
- Loading branch information
Showing
9 changed files
with
164 additions
and
20 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,108 @@ | ||
import Ember from 'ember'; | ||
import ObjectProxy from '@ember/object/proxy'; | ||
import { expectTypeOf } from 'expect-type'; | ||
|
||
interface Book { | ||
title: string; | ||
subtitle: string; | ||
chapters: Array<{ title: string }>; | ||
} | ||
|
||
class DefaultProxy extends ObjectProxy {} | ||
expectTypeOf(DefaultProxy.create().content).toEqualTypeOf<object | undefined>(); | ||
|
||
class BookProxy extends ObjectProxy<Book> { | ||
private readonly baz = 'baz'; | ||
|
||
altTitle = 'Alt'; | ||
|
||
getTitle() { | ||
return this.get('title'); | ||
} | ||
|
||
getPropertiesTitleSubtitle() { | ||
return this.getProperties('title', 'subtitle'); | ||
} | ||
} | ||
|
||
const book = BookProxy.create(); | ||
expectTypeOf(book.content).toEqualTypeOf<Book | undefined>(); | ||
|
||
// @ts-expect-error | ||
book.get('unknownProperty'); | ||
expectTypeOf(book.get('title')).toEqualTypeOf<string | undefined>(); | ||
expectTypeOf(book.get('altTitle')).toBeString(); | ||
expectTypeOf(book.getTitle()).toEqualTypeOf<string | undefined>(); | ||
|
||
// @ts-expect-error | ||
book.getProperties('title', 'unknownProperty'); | ||
expectTypeOf(book.getProperties('title', 'subtitle')).toEqualTypeOf< | ||
Pick<Partial<Book>, 'title' | 'subtitle'> | ||
>(); | ||
expectTypeOf(book.getPropertiesTitleSubtitle()).toEqualTypeOf< | ||
Pick<Partial<Book>, 'title' | 'subtitle'> | ||
>(); | ||
expectTypeOf(book.getProperties(['subtitle', 'chapters'])).toEqualTypeOf< | ||
Pick<Partial<Book>, 'subtitle' | 'chapters'> | ||
>(); | ||
// @ts-expect-error | ||
book.getProperties(['title', 'unknownProperty']); | ||
|
||
// @ts-expect-error | ||
book.get('baz'); | ||
|
||
book.set('title', 'New'); | ||
// @ts-expect-error | ||
book.set('title', 1); | ||
book.set('altTitle', 'Alternate'); | ||
// @ts-expect-error | ||
book.set('altTitle', 1); | ||
book.setProperties({ | ||
title: 'new', | ||
subtitle: 'and improved', | ||
altTitle: 'Alternate2', | ||
}); | ||
// @ts-expect-error | ||
book.setProperties({ title: 1 }); | ||
// @ts-expect-error | ||
book.setProperties({ altTitle: 1 }); | ||
// @ts-expect-error | ||
book.setProperties({ invalid: true }); | ||
|
||
class Person extends Ember.Object { | ||
firstName = 'Peter'; | ||
|
||
lastName = 'Wagenet'; | ||
|
||
@Ember.computed('firstName', 'lastName') | ||
get fullName() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
|
||
set fullName(value: string) { | ||
const [firstName, lastName] = value.split(' '); | ||
|
||
Ember.set(this, 'firstName', firstName ?? ''); | ||
Ember.set(this, 'lastName', lastName ?? ''); | ||
} | ||
} | ||
|
||
class PersonProxy extends ObjectProxy<Person> {} | ||
|
||
const person = PersonProxy.create(); | ||
|
||
expectTypeOf(person.get('firstName')).toEqualTypeOf<string | undefined>(); | ||
expectTypeOf(person.get('fullName')).toEqualTypeOf<string | undefined>(); | ||
expectTypeOf(person.set('fullName', 'John Doe')).toBeString(); | ||
// @ts-expect-error | ||
person.set('fullName', 1); | ||
// @ts-expect-error | ||
person.set('invalid', true); | ||
expectTypeOf(person.setProperties({ fullName: 'John Doe' })).toEqualTypeOf< | ||
Pick<PersonProxy & Person, 'fullName'> | ||
>(); | ||
expectTypeOf(person.setProperties({ fullName: 'John Doe' }).fullName).toBeString(); | ||
// @ts-expect-error | ||
person.setProperties({ fullName: 1 }); | ||
// @ts-expect-error | ||
person.setProperties({ fullName: 'John Doe', invalid: true }); |
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,16 @@ | ||
import { capabilities } from '@ember/component'; | ||
import { expectTypeOf } from 'expect-type'; | ||
|
||
expectTypeOf(capabilities('3.13')).toMatchTypeOf<{ | ||
asyncLifecycleCallbacks?: boolean | undefined; | ||
destructor?: boolean | undefined; | ||
updateHook?: boolean | undefined; | ||
}>(); | ||
|
||
capabilities('3.13', { asyncLifecycleCallbacks: true }); | ||
capabilities('3.4', { asyncLifecycleCallbacks: true }); | ||
|
||
// @ts-expect-error invalid capabilities | ||
capabilities('3.13', { asyncLifecycleCallbacks: 1 }); | ||
// @ts-expect-error invalid verison | ||
capabilities('3.12'); |
8 changes: 8 additions & 0 deletions
8
type-tests/preview/@ember/component-test/set-component-template.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,8 @@ | ||
import { setComponentTemplate } from '@ember/component'; | ||
import type { TemplateFactory } from 'htmlbars-inline-precompile'; | ||
import { expectTypeOf } from 'expect-type'; | ||
|
||
// Good enough for testing | ||
let factory = {} as TemplateFactory; | ||
|
||
expectTypeOf(setComponentTemplate(factory, {})).toEqualTypeOf<object>(); |
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