Glint
Search…
Component Signatures
Since the implementation of RFC 748, Glimmer and Ember components accept a Signature type parameter as part of their definition. This parameter is expected to be an object type with (up to) three members: Args, Element and Blocks.
Args represents the arguments your component accepts. Typically this will be an object type mapping the names of your args to their expected type. If no Args key is specified, it will be a type error to pass any arguments to your component.
The Element field declares what type of element(s), if any, the component applies its passed ...attributes to. This is often the component's root element. Tracking this type ensures any modifiers used on your component will be compatible with the DOM element(s) they're ultimately attached to. If no Element is specified, it will be a type error to set any HTML attributes or modifiers when invoking your component.
The Blocks field specifies the names of any blocks the component yields to, as well as the type of any parameter(s) those blocks will receive. If no Blocks key is specified, it will be a type error to invoke your component in block form.
Note that the inverse block is an alias for else. These should be defined in Blocks as else, though {{yield to="inverse"}} will continue to work.

Glimmer Components

app/components/super-table.ts
1
import Component from '@glimmer/component';
2
3
export interface SuperTableSignature<T> {
4
// We have a `<table>` as our root element
5
Element: HTMLTableElement;
6
// We accept an array of items, one per row
7
Args: {
8
items: Array<T>;
9
};
10
// We accept two named blocks: a parameter-less `header` block
11
// and a `row` block which will be invoked with each item and
12
// its index sequentially.
13
Blocks: {
14
header: [];
15
row: [item: T, index: number];
16
};
17
}
18
19
export default class SuperTable<T> extends Component<SuperTableSignature<T>> {}
Copied!
app/components/super-table.hbs
1
<table ...attributes>
2
{{#if (has-block 'header')}}
3
<thead>
4
<tr>{{yield to='header'}}</tr>
5
</thead>
6
{{/if}}
7
8
<tbody>
9
{{#each @items as |item index|}}
10
<tr>{{yield item index to='row'}}</tr>
11
{{/each}}
12
</tbody>
13
</table>
Copied!

Ember Components

Since Ember components don't have this.args, it takes slightly more boilerplate to make them typesafe.
app/components/greeting.ts
1
import Component from '@ember/component';
2
import { computed } from '@ember/object';
3
4
export interface GreetingSignature {
5
Args: {
6
message: string;
7
target?: string;
8
};
9
Blocks: {
10
default: [greeting: string];
11
};
12
}
13
14
// We define this type alias so that we can extend it below:
15
type GreetingArgs = GreetingSignature['Args'];
16
17
// This line declares that our component's args will be 'splatted' on to the instance:
18
export default interface Greeting extends GreetingArgs {}
19
export default class Greeting extends Component<GreetingSignature> {
20
@computed('target')
21
private get greetingTarget() {
22
// Therefore making `this.target` a legal `string | undefined` property access:
23
return this.target ?? 'World';
24
}
25
}
Copied!
app/components/greeting.hbs
1
{{yield (concat @message ', ' this.greetingTarget '!')}}
Copied!
Ember components also support positional arguments in their signature. Such usage is relatively rare, but components such as {{animated-if}} do take advantage of it.
app/components/greeting.ts
1
// ...
2
3
export interface GreetingSignature {
4
Args: {
5
Named: {
6
message: string;
7
target?: string;
8
};
9
Positional: [extraSpecialPreamble: string];
10
};
11
Blocks: {
12
default: [greeting: string];
13
};
14
}
15
16
type GreetingArgs = GreetingSignature['Args']['Named'];
17
18
export default interface Greeting extends GreetingArgs {}
19
export default class Greeting extends Component<GreetingSignature> {
20
static positionalParams = ['extraSpecialPreamble'];
21
declare readonly extraSpecialPreamble: string;
22
// ...
23
}
Copied!
Positional args are specified as a Positional tuple nested within Args, the same way they are in helper and modifier signatures. You can also specify positional args with ComponentLike types in this way.
Note that both Positional args and the Element type are not fully integrated with the string-based APIs on the @ember/component base class. This means, for example, that there's no enforcement that tagName = 'table' and Element: HTMLTableElement are actually correlated to one another.
Copy link
Edit on GitHub