Skip to content

Commit 420bdbc

Browse files
committed
attempt to add failing type tests
1 parent 113c42a commit 420bdbc

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

packages/template/-private/signature.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export type ComponentSignatureBlocks<S> = S extends { Blocks: infer Blocks }
4545

4646
/** Given a component signature `S`, get back the `Element` type. */
4747
export type ComponentSignatureElement<S> = S extends { Element: infer Element }
48-
? Element extends null
48+
? NonNullable<Element> extends never
49+
// ? Element extends null
4950
? unknown
5051
: Element
5152
: unknown;

packages/template/__tests__/signature.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ComponentSignatureBlocks,
55
ComponentSignatureElement,
66
} from '../-private/signature';
7+
import { ComponentLike } from '../';
78

89
type LegacyArgs = {
910
foo: number;
@@ -153,3 +154,56 @@ interface FullLongSig {
153154
expectTypeOf<ComponentSignatureArgs<FullLongSig>>().toEqualTypeOf<FullLongSig['Args']>();
154155
expectTypeOf<ComponentSignatureBlocks<FullLongSig>>().toEqualTypeOf<FullLongSig['Blocks']>();
155156
expectTypeOf<ComponentSignatureElement<FullLongSig>>().toEqualTypeOf<FullLongSig['Element']>();
157+
158+
// types to simulate the `(element)` helper
159+
// Issue: https://github.com/typed-ember/glint/issues/610
160+
type ElementFromTagName<T extends string> = T extends keyof HTMLElementTagNameMap
161+
? HTMLElementTagNameMap[T]
162+
: Element;
163+
type ElementHelperPositional<T extends string> = [name: T];
164+
type ElementHelperReturn<T extends string> = ComponentLike<{
165+
Element: ElementFromTagName<T>;
166+
Blocks: { default: [] };
167+
}>;
168+
169+
interface ElementSignature<T extends string> {
170+
Args: {
171+
Positional: ElementHelperPositional<T>;
172+
};
173+
Return: ElementHelperReturn<T> | undefined;
174+
}
175+
176+
// signature for a component receiving an `(element)`
177+
interface ElementReceiverSignature<T extends string> {
178+
Element: ElementFromTagName<T>;
179+
Args: {
180+
element: ElementSignature<T>['Return'];
181+
};
182+
Blocks: {
183+
default: [];
184+
};
185+
}
186+
187+
expectTypeOf<ComponentSignatureArgs<ElementReceiverSignature<'div'>>>().toEqualTypeOf<{
188+
Named: {
189+
element: ElementSignature<'div'>['Return'];
190+
};
191+
Positional: []
192+
}>();
193+
194+
expectTypeOf<ComponentSignatureArgs<ElementReceiverSignature<null>>>().toEqualTypeOf<{
195+
Named: {
196+
element: {
197+
Args: {
198+
Positional: ElementHelperPositional<never>;
199+
};
200+
Return: ComponentLike<{
201+
Element: Element;
202+
Blocks: { default: [] };
203+
}> | undefined;
204+
} | undefined;
205+
};
206+
Positional: []
207+
}>();
208+
expectTypeOf<ComponentSignatureElement<ElementReceiverSignature<'div'>>>().toEqualTypeOf<HTMLDivElement>();
209+

0 commit comments

Comments
 (0)