Skip to content

Commit

Permalink
Add explicit types tests for ExpandSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreeman committed Apr 8, 2022
1 parent 2f7de9d commit 2c03aae
Showing 1 changed file with 98 additions and 2 deletions.
100 changes: 98 additions & 2 deletions test/types/component-test.ts
Expand Up @@ -12,7 +12,10 @@ import Component from '@glimmer/component';
// matches the actual import location to which this type would be emitted. Since
// this is an internal-only type whose presence consumers should not rely on and
// which they should not use in any way, this is "safe" from a public API POV.
import { EmptyObject } from '@glimmer/component/dist/types/addon/-private/component';
import {
EmptyObject,
ExpandSignature,
} from '@glimmer/component/dist/types/addon/-private/component';

declare let basicComponent: Component;
expectTypeOf(basicComponent).toHaveProperty('args');
Expand All @@ -33,15 +36,37 @@ type LegacyArgs = {
const componentWithLegacyArgs = new Component<LegacyArgs>({}, { foo: 123 });
expectTypeOf(componentWithLegacyArgs.args).toEqualTypeOf<Readonly<LegacyArgs>>();

expectTypeOf<ExpandSignature<LegacyArgs>>().toEqualTypeOf<{
Args: { Named: LegacyArgs; Positional: [] };
Element: null;
Blocks: EmptyObject;
}>();

// Here, we are testing that the types propertly distribute over union types,
// generics which extend other types, etc.
type LegacyArgsDistributive = { foo: number } | { bar: string; baz: boolean };

const legacyArgsDistributiveA = new Component<LegacyArgsDistributive>({}, { foo: 123 });
expectTypeOf(legacyArgsDistributiveA.args).toEqualTypeOf<Readonly<LegacyArgsDistributive>>();
const legacyArgsDistributiveB = new Component<LegacyArgsDistributive>({}, { bar: "hello", baz: true });
const legacyArgsDistributiveB = new Component<LegacyArgsDistributive>(
{},
{ bar: 'hello', baz: true }
);
expectTypeOf(legacyArgsDistributiveB.args).toEqualTypeOf<Readonly<LegacyArgsDistributive>>();

expectTypeOf<ExpandSignature<LegacyArgsDistributive>>().toEqualTypeOf<
| {
Args: { Named: { foo: number }; Positional: [] };
Element: null;
Blocks: EmptyObject;
}
| {
Args: { Named: { bar: string; baz: boolean }; Positional: [] };
Element: null;
Blocks: EmptyObject;
}
>();

interface ExtensibleLegacy<T> {
value: T;
extras: boolean;
Expand All @@ -67,6 +92,12 @@ interface ArgsOnly {
const componentWithArgsOnly = new Component<ArgsOnly>({}, { foo: 123 });
expectTypeOf(componentWithArgsOnly.args).toEqualTypeOf<Readonly<LegacyArgs>>();

expectTypeOf<ExpandSignature<ArgsOnly>>().toEqualTypeOf<{
Args: { Named: LegacyArgs; Positional: [] };
Element: null;
Blocks: EmptyObject;
}>();

interface ElementOnly {
Element: HTMLParagraphElement;
}
Expand All @@ -75,6 +106,12 @@ const componentWithElOnly = new Component<ElementOnly>({}, {});

expectTypeOf(componentWithElOnly.args).toEqualTypeOf<Readonly<EmptyObject>>();

expectTypeOf<ExpandSignature<ElementOnly>>().toEqualTypeOf<{
Args: { Named: EmptyObject; Positional: [] };
Element: HTMLParagraphElement;
Blocks: EmptyObject;
}>();

interface Blocks {
default: [string];
inverse: [];
Expand All @@ -88,6 +125,23 @@ const componentWithBlockOnly = new Component<BlockOnlySig>({}, {});

expectTypeOf(componentWithBlockOnly.args).toEqualTypeOf<Readonly<EmptyObject>>();

expectTypeOf<ExpandSignature<BlockOnlySig>>().toEqualTypeOf<{
Args: { Named: EmptyObject; Positional: [] };
Element: null;
Blocks: {
default: {
Params: {
Positional: [string];
};
};
inverse: {
Params: {
Positional: [];
};
};
};
}>();

interface ArgsAndBlocks {
Args: LegacyArgs;
Blocks: Blocks;
Expand All @@ -96,6 +150,23 @@ interface ArgsAndBlocks {
const componentwithArgsAndBlocks = new Component<ArgsAndBlocks>({}, { foo: 123 });
expectTypeOf(componentwithArgsAndBlocks.args).toEqualTypeOf<Readonly<LegacyArgs>>();

expectTypeOf<ExpandSignature<ArgsAndBlocks>>().toEqualTypeOf<{
Args: { Named: LegacyArgs; Positional: [] };
Element: null;
Blocks: {
default: {
Params: {
Positional: [string];
};
};
inverse: {
Params: {
Positional: [];
};
};
};
}>();

interface ArgsAndEl {
Args: LegacyArgs;
Element: HTMLParagraphElement;
Expand All @@ -104,6 +175,12 @@ interface ArgsAndEl {
const componentwithArgsAndEl = new Component<ArgsAndEl>({}, { foo: 123 });
expectTypeOf(componentwithArgsAndEl.args).toEqualTypeOf<Readonly<LegacyArgs>>();

expectTypeOf<ExpandSignature<ArgsAndEl>>().toEqualTypeOf<{
Args: { Named: LegacyArgs; Positional: [] };
Element: HTMLParagraphElement;
Blocks: EmptyObject;
}>();

interface FullShortSig {
Args: LegacyArgs;
Element: HTMLParagraphElement;
Expand All @@ -113,6 +190,23 @@ interface FullShortSig {
const componentWithFullShortSig = new Component<FullShortSig>({}, { foo: 123 });
expectTypeOf(componentWithFullShortSig.args).toEqualTypeOf<Readonly<LegacyArgs>>();

expectTypeOf<ExpandSignature<FullShortSig>>().toEqualTypeOf<{
Args: { Named: LegacyArgs; Positional: [] };
Element: HTMLParagraphElement;
Blocks: {
default: {
Params: {
Positional: [string];
};
};
inverse: {
Params: {
Positional: [];
};
};
};
}>();

interface FullLongSig {
Args: {
Named: LegacyArgs;
Expand All @@ -130,3 +224,5 @@ interface FullLongSig {

const componentWithFullSig = new Component<FullLongSig>({}, { foo: 123 });
expectTypeOf(componentWithFullSig.args).toEqualTypeOf<Readonly<LegacyArgs>>();

expectTypeOf<ExpandSignature<FullLongSig>>().toEqualTypeOf<FullLongSig>();

0 comments on commit 2c03aae

Please sign in to comment.