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

[BUGFIX release] Add some missing types to preview types #20274

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions type-tests/preview/@ember/component-test/capabilities.ts
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');
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>();
Comment on lines +5 to +8
Copy link
Contributor

Choose a reason for hiding this comment

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

Two alternative approaches that work well for spots like this:

  1. Use declare:

    Suggested change
    // Good enough for testing
    let factory = {} as TemplateFactory;
    expectTypeOf(setComponentTemplate(factory, {})).toEqualTypeOf<object>();
    declare let factory: TemplateFactory
    expectTypeOf(setComponentTemplate(factory, {})).toEqualTypeOf<object>();
  2. Test the parameters and return type explicitly instead:

    Suggested change
    // Good enough for testing
    let factory = {} as TemplateFactory;
    expectTypeOf(setComponentTemplate(factory, {})).toEqualTypeOf<object>();
    expectTypeOf(setComponentTemplate).parameters.toEqualTypeOf<[TemplateFactory, object]>();
    expectTypeOf(setComponentTemplate).returns.toEqualTypeOf<object>();

Both of these avoid needing the cast, which is not a particularly big deal here but is nice in the "modeling what we want to see" sense.

12 changes: 0 additions & 12 deletions types/preview/@ember/application/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,4 @@ declare module '@ember/application' {
* @deprecated Use `import { setOwner } from '@ember/owner';` instead.
*/
export function setOwner(object: object, owner: Owner): void;

/**
* Detects when a specific package of Ember (e.g. 'Ember.Application')
* has fully loaded and is available for extension.
*/
export function onLoad(name: string, callback: AnyFn): unknown;

/**
* Called when an Ember.js package (e.g Ember.Application) has finished
* loading. Triggers any callbacks registered for this event.
*/
export function runLoadHooks(name: string, object?: {}): unknown;
}
39 changes: 31 additions & 8 deletions types/preview/@ember/component/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,37 @@ declare module '@ember/component' {
object: T
): T;

/**
* Takes a component class and returns the template associated with the given component class,
* if any, or one of its superclasses, if any, or undefined if no template association was found.
*
* @param object the component object
* @return the template factory of the given component
*/
export function getComponentTemplate(obj: object): TemplateFactory | undefined;
/**
* Takes a component class and returns the template associated with the given component class,
* if any, or one of its superclasses, if any, or undefined if no template association was found.
*
* @param object the component object
* @return the template factory of the given component
*/
export function getComponentTemplate(obj: object): TemplateFactory | undefined;

export function setComponentTemplate(factory: TemplateFactory, obj: object): object;

interface ComponentCapabilitiesVersions {
'3.4': {
asyncLifecycleCallbacks?: boolean;
destructor?: boolean;
};

'3.13': {
asyncLifecycleCallbacks?: boolean;
destructor?: boolean;
updateHook?: boolean;
};
}

interface ComponentCapabilities extends Capabilities {
asyncLifeCycleCallbacks: boolean;
destructor: boolean;
updateHook: boolean;
}

export function capabilities<Version extends keyof ComponentCapabilitiesVersions>(managerAPI: Version, options?: ComponentCapabilitiesVersions[Version]): ComponentCapabilities;

// In normal TypeScript, these built-in components are essentially opaque tokens
// that just need to be importable. Declaring them with unique interfaces
Expand Down