From 753390a8b6eeb3f129f28e566f558c2fac3240f3 Mon Sep 17 00:00:00 2001 From: Thomas Gossmann Date: Thu, 17 Aug 2023 21:42:45 +0200 Subject: [PATCH 1/2] Allow generics in Signatures --- packages/template/-private/signature.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/template/-private/signature.d.ts b/packages/template/-private/signature.d.ts index 7d525fe24..dbea75e7e 100644 --- a/packages/template/-private/signature.d.ts +++ b/packages/template/-private/signature.d.ts @@ -45,7 +45,7 @@ export type ComponentSignatureBlocks = S extends { Blocks: infer Blocks } /** Given a component signature `S`, get back the `Element` type. */ export type ComponentSignatureElement = S extends { Element: infer Element } - ? NonNullable extends never + ? Element extends null ? unknown : Element : unknown; From f79c9c5160c6c0563920def28ee8a6fe75a3f9b0 Mon Sep 17 00:00:00 2001 From: gossi Date: Sat, 9 Sep 2023 17:40:29 +0200 Subject: [PATCH 2/2] attempt to add failing type tests --- packages/template/-private/signature.d.ts | 3 +- packages/template/__tests__/signature.test.ts | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/template/-private/signature.d.ts b/packages/template/-private/signature.d.ts index dbea75e7e..00baf4481 100644 --- a/packages/template/-private/signature.d.ts +++ b/packages/template/-private/signature.d.ts @@ -45,7 +45,8 @@ export type ComponentSignatureBlocks = S extends { Blocks: infer Blocks } /** Given a component signature `S`, get back the `Element` type. */ export type ComponentSignatureElement = S extends { Element: infer Element } - ? Element extends null + ? NonNullable extends never +// ? Element extends null ? unknown : Element : unknown; diff --git a/packages/template/__tests__/signature.test.ts b/packages/template/__tests__/signature.test.ts index fa59161d1..d696a3f44 100644 --- a/packages/template/__tests__/signature.test.ts +++ b/packages/template/__tests__/signature.test.ts @@ -4,6 +4,7 @@ import { ComponentSignatureBlocks, ComponentSignatureElement, } from '../-private/signature'; +import { ComponentLike } from '../'; type LegacyArgs = { foo: number; @@ -153,3 +154,56 @@ interface FullLongSig { expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); + +// types to simulate the `(element)` helper +// Issue: https://github.com/typed-ember/glint/issues/610 +type ElementFromTagName = T extends keyof HTMLElementTagNameMap + ? HTMLElementTagNameMap[T] + : Element; +type ElementHelperPositional = [name: T]; +type ElementHelperReturn = ComponentLike<{ + Element: ElementFromTagName; + Blocks: { default: [] }; +}>; + +interface ElementSignature { + Args: { + Positional: ElementHelperPositional; + }; + Return: ElementHelperReturn | undefined; +} + +// signature for a component receiving an `(element)` +interface ElementReceiverSignature { + Element: ElementFromTagName; + Args: { + element: ElementSignature['Return']; + }; + Blocks: { + default: []; + }; +} + +expectTypeOf>>().toEqualTypeOf<{ + Named: { + element: ElementSignature<'div'>['Return']; + }; + Positional: [] +}>(); + +expectTypeOf>>().toEqualTypeOf<{ + Named: { + element: { + Args: { + Positional: ElementHelperPositional; + }; + Return: ComponentLike<{ + Element: Element; + Blocks: { default: [] }; + }> | undefined; + } | undefined; + }; + Positional: [] +}>(); +expectTypeOf>>().toEqualTypeOf(); +