You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -643,50 +643,45 @@ Nearly anything you can do with a “regular” TypeScript function or class, yo
643
643
We can make a component accept a [generic][generic] type, or use [union][union] types.
644
644
With these tools at our disposal, we can even define our signatures to [make illegal states un-representable][illegal].
645
645
646
-
To see this in practice, consider a list component which yields back out instances of the same type it provides, and provides the appropriate element target based on a `type` argument.
647
-
Yielding back out the same type passed in will use generics, and providing an appropriate element target for `...attributes` can use a union type.
646
+
### Union Types
647
+
648
+
To see this in practice, consider a list component that provides the appropriate element target based on a `type` argument.
648
649
649
650
Here is how that might look, using a class-backed component rather than a template-only component, since the only places TypeScript allows us to name new generic types are on functions and classes:
@@ -701,17 +696,134 @@ function isOrdered(type: 'ordered' | 'unordered'): type is 'ordered' {
701
696
If you are using Glint, when this component is invoked, the `@type` argument will determine what kinds of modifiers are legal to apply to it. For example, if you defined a modifier `reverse` which required an `HTMLOListElement`, this invocation would be rejected:
The same approach with generics works for class-based helpers and class-based modifiers.
711
-
Function-based helpers and modifiers can also use generics, but by using them on the function definition rather than via a signature.
712
702
One caveat: particularly complicated union types in signatures can sometimes become too complex for Glint/TypeScript to resolve when invoking in a template.
713
703
In those cases, your best bet is to find a simpler way to structure the types while preserving type safety.
714
704
705
+
### Generic Types
706
+
You can use generic types to improve Intellisense and type checking for consumers of your component:
{{person.username}} {{!-- This will throw a type error because 'username' is not defined on our items --}}
750
+
{{person.name}}
751
+
</List>
752
+
</template>
753
+
```
754
+
755
+
Function-based helpers and modifiers can also use generics, but by using them on the function definition rather than via a signature.
756
+
The same approach with generics works for class-based helpers and class-based modifiers.
757
+
758
+
You can also use generic types when yielding a contextual component by creating a property on the class that implements the generic type on the relevant component:
When consuming this component, and it's yielded contextual component, Glint will again infer the type of the yielded value to be the same as the type of `@items`:
0 commit comments