Skip to content

Commit

Permalink
[BUGFIX types] Add missing type declarations in the preview types for…
Browse files Browse the repository at this point in the history
… @ember/template-compilation
  • Loading branch information
NullVoxPopuli committed Mar 16, 2023
1 parent 0c5518e commit 9da91e8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions type-tests/@ember/template-compilation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { precompileTemplate } from '@ember/template-compilation';
import { setComponentTemplate } from '@ember/component';
import templateOnly from '@ember/component/template-only';

import { expectTypeOf } from 'expect-type';

// Valid usages
precompileTemplate(`Hello World`, { moduleName: 'foo' });
precompileTemplate(`Hello World`, { strictMode: true });
precompileTemplate(`Hello World`, { strictMode: false });
precompileTemplate(`Hello World`, { strictMode: true, scope: () => ({}) });
precompileTemplate(`Hello World`, { strictMode: true, scope: () => ({ setComponentTemplate }) });
precompileTemplate(`Hello World`, { strictMode: true, moduleName: 'hello', scope: () => ({}) });

// Integration, since this is the primary use case for precompileTemplate
expectTypeOf(setComponentTemplate(precompileTemplate(`Hello World`), templateOnly())).toBeObject();

// @ts-expect-error scope must be a function
precompileTemplate(`Hello World`, { strictMode: true, scope: {} });

// @ts-expect-error scope must return an object
precompileTemplate(`Hello World`, { strictMode: true, scope: () => {} });

// @ts-expect-error scope must return an object and arrays are not the kind of object we want
precompileTemplate(`Hello World`, { strictMode: true, scope: () => [] });

// @ts-expect-error scope has no purpose when strictMode is false
precompileTemplate(`Hello World`, { strictMode: false, scope: () => ({}) });
26 changes: 26 additions & 0 deletions types/preview/@ember/template-compilation/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* References:
* - https://github.com/emberjs/rfcs/pull/496
* - https://github.com/emberjs/rfcs/pull/779
*/
declare module '@ember/template-compilation' {
interface TemplateFactory {
__htmlbars_inline_precompile_template_factory: any;
}

type PrecompileTemplateOptions =
| {
strictMode?: false;
moduleName?: string;
}
| {
strictMode: true;
moduleName?: string;
scope?: () => Record<string, unknown>;
};

export function precompileTemplate(
template: string,
options?: PrecompileTemplateOptions
): TemplateFactory;
}
2 changes: 2 additions & 0 deletions types/preview/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ import './@ember/string';
import './@ember/template';
import './@ember/template/-private/handlebars';

import './@ember/template-compilation';

import './@ember/test';
import './@ember/test/adapter';

Expand Down

0 comments on commit 9da91e8

Please sign in to comment.