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

Commit aeb1213

Browse files
committed
Fix ExpandSignature & ArgsFor
1 parent 4707c1c commit aeb1213

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function setDestroyed(component: GlimmerComponent<object>): void {
1717
interface ArgsSetMap extends WeakMap<Args<unknown>, boolean> {
1818
get<S>(key: Args<S>): boolean | undefined;
1919
set<S>(key: Args<S>, value: boolean): this;
20+
has<S>(key: Args<S>): boolean;
2021
}
2122

2223
// SAFETY: this only holds because we *only* acces this when `DEBUG` is `true`.
@@ -48,11 +49,13 @@ declare const Empty: unique symbol;
4849
*/
4950
export type EmptyObject = { [Empty]?: true };
5051

51-
type GetOrElse<Obj, K, Fallback> = K extends keyof Obj ? Obj[K] : Fallback;
52+
type GetOrElse<Obj, K extends PropertyKey, Fallback> = Obj extends { [Key in K]: infer U }
53+
? U
54+
: Fallback;
5255

5356
/** Given a signature `S`, get back the `Args` type. */
54-
type ArgsFor<S> = 'Args' extends keyof S
55-
? S['Args'] extends { Named?: object; Positional?: unknown[] } // Are they longhand already?
57+
type ArgsFor<S> = S extends { Args: infer Args }
58+
? Args extends { Named?: object; Positional?: unknown[] } // Are they longhand already?
5659
? {
5760
Named: GetOrElse<S['Args'], 'Named', EmptyObject>;
5861
Positional: GetOrElse<S['Args'], 'Positional', []>;
@@ -65,11 +68,11 @@ type _ExpandSignature<T> = {
6568
Args: keyof T extends 'Args' | 'Element' | 'Blocks' // Is this a `Signature`?
6669
? ArgsFor<T> // Then use `Signature` args
6770
: { Named: T; Positional: [] }; // Otherwise fall back to classic `Args`.
68-
Blocks: 'Blocks' extends keyof T
71+
Blocks: T extends { Blocks: infer Blocks }
6972
? {
70-
[Block in keyof T['Blocks']]: T['Blocks'][Block] extends unknown[]
71-
? { Params: { Positional: T['Blocks'][Block] } }
72-
: T['Blocks'][Block];
73+
[Block in keyof Blocks]: Blocks[Block] extends unknown[]
74+
? { Params: { Positional: Blocks[Block] } }
75+
: Blocks[Block];
7376
}
7477
: EmptyObject;
7578
};
@@ -230,8 +233,8 @@ export default class GlimmerComponent<S = unknown> {
230233
* @param owner
231234
* @param args
232235
*/
233-
constructor(_owner: unknown, args: Args<S>) {
234-
if (DEBUG && !ARGS_SET.has(args)) {
236+
constructor(owner: unknown, args: Args<S>) {
237+
if (DEBUG && !(owner !== null && typeof owner === 'object' && ARGS_SET.has(args))) {
235238
throw new Error(
236239
`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.`
237240
);

0 commit comments

Comments
 (0)