Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit ad429d5

Browse files
committed
Fix type distributivity in Glimmer Component Signature
When using a `keyof` type to check whether the type parameter for Glimmer Component is a `Signature` or the classic `Args`-only type, if we do not force TS to distribute over union types, it resolves the `keyof` check for union types with no shared members as `never`, and `never extends <anything>` is always true. This in turn meant that for all such unions, as well as for cases where users were providing generic types which could then be further extended in their own subclasses. Accordingly, introduce the standard technique TypeScript provides for opting into distributivity: conditional types are documented to support exactly this.
1 parent 9c9f023 commit ad429d5

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

packages/@glimmer/component/addon/-private/component.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,7 @@ type ArgsFor<S> = S extends { Args: infer Args }
5454
: { Named: S['Args']; Positional: [] }
5555
: { Named: EmptyObject; Positional: [] };
5656

57-
/**
58-
* Given any allowed shorthand form of a signature, desugars it to its full
59-
* expanded type.
60-
*
61-
* @internal This is only exported so we can avoid duplicating it in
62-
* [Glint](https://github.com/typed-ember/glint) or other such tooling. It is
63-
* *not* intended for public usage, and the specific mechanics it uses may
64-
* change at any time. Although the signature produced by is part of Glimmer's
65-
* public API the existence and mechanics of this specific symbol are *not*,
66-
* so ***DO NOT RELY ON IT***.
67-
*/
68-
export type ExpandSignature<T> = {
57+
type _ExpandSignature<T> = {
6958
Element: GetOrElse<T, 'Element', null>;
7059
Args: keyof T extends 'Args' | 'Element' | 'Blocks' // Is this a `Signature`?
7160
? ArgsFor<T> // Then use `Signature` args
@@ -74,11 +63,29 @@ export type ExpandSignature<T> = {
7463
? {
7564
[Block in keyof Blocks]: Blocks[Block] extends unknown[]
7665
? { Positional: Blocks[Block] }
77-
: Blocks[Block];
66+
: Blocks[Block]
7867
}
7968
: EmptyObject;
8069
};
8170

71+
/**
72+
* Given any allowed shorthand form of a signature, desugars it to its full
73+
* expanded type.
74+
*
75+
* @internal This is only exported so we can avoid duplicating it in
76+
* [Glint](https://github.com/typed-ember/glint) or other such tooling. It is
77+
* *not* intended for public usage, and the specific mechanics it uses may
78+
* change at any time. Although the signature produced by is part of Glimmer's
79+
* public API the existence and mechanics of this specific symbol are *not*,
80+
* so ***DO NOT RELY ON IT***.
81+
*/
82+
// The conditional type here is because TS applies conditional types
83+
// distributively. This means that for union types, checks like `keyof T` get
84+
// all the keys from all elements of the union, instead of ending up as `never`
85+
// and then always falling into the `Signature` path instead of falling back to
86+
// the legacy args handling path.
87+
export type ExpandSignature<T> = T extends any ? _ExpandSignature<T> : never;
88+
8289
/**
8390
* @internal we use this type for convenience internally; inference means users
8491
* should not normally need to name it
@@ -220,7 +227,9 @@ export default class BaseComponent<S = unknown> {
220227
constructor(owner: unknown, args: Args<S>) {
221228
if (DEBUG && !(owner !== null && typeof owner === 'object' && ARGS_SET.has(args))) {
222229
throw new Error(
223-
`You must pass both the owner and args to super() in your component: ${this.constructor.name}. You can pass them directly, or use ...arguments to pass all arguments through.`
230+
`You must pass both the owner and args to super() in your component: ${
231+
this.constructor.name
232+
}. You can pass them directly, or use ...arguments to pass all arguments through.`
224233
);
225234
}
226235

0 commit comments

Comments
 (0)