Skip to content

Commit 517502e

Browse files
authored
Add type definition for test helper import (#481)
* Add type definition for test helper import * types: add support for `hbs(template, options)` invocation * types: improve hbs usage examples in docstring * Drop weirdly-added tests file * types: explicitly set location in package.json
1 parent be3c14d commit 517502e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

index.d.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Using the same "brand" as the types for `htmlbars-inline-precompile` for
2+
// backwards compatibility. The actual value of the brand doesn't matter; it is
3+
// only important that it (a) is distinct and (b) interoperates with existing
4+
// uses of the `hbs` export from `htmlbars-inline-precompile` [1].
5+
//
6+
// [1]: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/24a21e0b8ec7eccdec781d7513bfa5947f1c6e20/types/ember/index.d.ts#L540:L542
7+
//
8+
// Note that we *intentionally* do not export this; the details are irrelevant
9+
// to consumers. The point is simply to have a *distinct* type that is therefore
10+
// not substitutable for just any other type.
11+
interface TemplateFactory {
12+
__htmlbars_inline_precompile_template_factory: any;
13+
}
14+
15+
export interface PrecompileOptions {
16+
moduleName?: string;
17+
parseOptions?: {
18+
srcName?: string;
19+
};
20+
}
21+
22+
/**
23+
* A helper for rendering components.
24+
*
25+
* @param tagged The template to render.
26+
*
27+
* ## Usage
28+
*
29+
* ### With tagged template
30+
*
31+
* ```ts
32+
* import { module, test } from 'qunit';
33+
* import { setupRenderingTest } from 'ember-qunit';
34+
* import { render } from '@ember/test-helpers';
35+
* import { hbs } from 'ember-cli-htmlbars';
36+
*
37+
* module('demonstrate hbs usage', function(hooks) {
38+
* setupRenderingTest(hooks);
39+
*
40+
* test('you can render things', function(assert) {
41+
* await render(hbs`<TestingComponents @isCool={{true}} />`);
42+
* assert.ok(true);
43+
* });
44+
* });
45+
* ```
46+
*
47+
* ## With string and options
48+
*
49+
* ```ts
50+
* import Component from '@glimmer/component';
51+
* import { setComponentTemplate } from '@ember/component';
52+
* import { hbs } from 'ember-cli-htmlbars';
53+
*
54+
* class Hello extends Component {
55+
* greeting = 'hello world';
56+
* }
57+
*
58+
* setComponentTemplate(
59+
* hbs('<p>{{this.greeting}}</p>', { moduleName: 'hello.hbs' }),
60+
* MyComponent
61+
* );
62+
* ```
63+
*/
64+
export function hbs(template: string, options?: PrecompileOptions): TemplateFactory;
65+
export function hbs(tagged: TemplateStringsArray): TemplateFactory;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib/"
2121
],
2222
"main": "lib/index.js",
23+
"types": "index.d.ts",
2324
"scripts": {
2425
"build": "ember build",
2526
"lint:hbs": "ember-template-lint .",

0 commit comments

Comments
 (0)