@glint/type-test 0.9.7 → 1.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # `@glint/type-test`
2
+
3
+ This library contains template helpers for testing inferred types in Glint-enabled templates.
4
+ It's similar in concept to (and built on) the [`expect-type`][et] library.
5
+
6
+ [et]: https://github.com/mmkal/expect-type
7
+
8
+ ## Usage
9
+
10
+ ```handlebars
11
+ {{expectTypeOf "hello" to.beString}}
12
+ {{expectTypeOf 123 to.equalTypeOf 456}}
13
+ ```
14
+
15
+ ### Strict-Mode Templates
16
+
17
+ For strict-mode templates, the `expectTypeOf` helper and `to` collection of expectations are
18
+ both directly importable from `@glint/type-test`.
19
+
20
+ ```tsx
21
+ import { expectTypeOf, to } from '@glint/type-test';
22
+
23
+ let letters = ['a', 'b', 'c'];
24
+
25
+ <template>
26
+ {{#each letters as |letter index|}}
27
+ {{expectTypeOf letter to.beString}}
28
+ {{expectTypeOf index to.beNumber}}
29
+ {{/each}}
30
+ <template>
31
+ ```
32
+
33
+ ### Loose-Mode Templates
34
+
35
+ For "classic" loose-mode Ember templates, `@glint/type-test` provides a `typeTest` wrapper that
36
+ will pass `expectTypeOf` and `to` to your template as args:
37
+
38
+ ```tsx
39
+ import { typeTest } from '@glint/type-test';
40
+ import { hbs } from 'ember-cli-htmlbars';
41
+
42
+ typeTest(
43
+ hbs`
44
+ {{#each (array 'a' 'b' 'c') as |letter index|}}
45
+ {{@expectTypeOf letter @to.beString}}
46
+ {{@expectTypeOf index @to.beNumber}}
47
+ {{/each}}
48
+ `
49
+ );
50
+ ```
51
+
52
+ You can also optionally provide an initial `this` value to `typeTest` to make values available
53
+ in your template to either test inference against or to use as a basis for comparing type equality.
54
+
55
+ ```tsx
56
+ import { typeTest } from '@glint/type-test';
57
+ import { hbs } from 'ember-cli-htmlbars';
58
+
59
+ typeTest(
60
+ { letters: ['a', 'b', 'c'] },
61
+ hbs`
62
+ {{#each this.letters as |letter index|}}
63
+ {{@expectTypeOf letter @to.beString}}
64
+ {{@expectTypeOf index @to.beNumber}}
65
+ {{/each}}
66
+ `
67
+ );
68
+ ```
69
+
70
+ ### Expectations
71
+
72
+ This library provides a set of expectations to compare the type of a given value to common simple
73
+ types.
74
+
75
+ ```tsx
76
+ import { expectTypeOf, to } from '@glint/type-test';
77
+
78
+ let symbolValue = Symbol('hi');
79
+ let anyValue: any = null;
80
+ let unknownValue: unknown = null;
81
+ let neverValue: never = (null as never);
82
+
83
+ <template>
84
+ {{expectTypeOf "hello" to.beString}}
85
+ {{expectTypeOf 123 to.beNumber}}
86
+ {{expectTypeOf true to.beBoolean}}
87
+ {{expectTypeOf symbolValue to.beSymbol}}
88
+ {{expectTypeOf anyValue to.beAny}}
89
+ {{expectTypeOf unknownValue to.beUnknown}}
90
+ {{expectTypeOf neverValue to.beNever}}
91
+ {{expectTypeOf null to.beNull}}
92
+ {{expectTypeOf undefined to.beUndefined}}
93
+ </template>
94
+ ```
95
+
96
+ It also provides expectations that allow you to compare the type of one value to that of another.
97
+
98
+ ```tsx
99
+ import { expectTypeOf, to } from '@glint/type-test';
100
+
101
+ let a: 'a' | 'b' = 'a';
102
+ let b: 'a' | 'b' = 'b';
103
+ let hi: string = 'hi';
104
+
105
+ <template>
106
+ <!-- 'a' | 'b' is the same type as itself -->
107
+ {{expectTypeOf a to.equalTypeOf b}}
108
+ <!-- 'a' | 'b' is assignable to the more general type string -->
109
+ {{expectTypeOf a to.beAssignableToTypeOf hi}}
110
+
111
+ <!-- string is not the same type as 'a' | 'b' -->
112
+ {{! @glint-expect-error }}
113
+ {{expectTypeOf hi to.beEqualTypeOf a}}
114
+
115
+ <!-- string is not assignable to 'a' | 'b' -->
116
+ {{! @glint-expect-error }}
117
+ {{expectTypeOf hi to.beAssignableToTypeOf a}}
118
+ </template>
119
+ ```
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DirectInvokable, EmptyObject, HasContext } from '@glint/template/-private/integration';
1
+ import { DirectInvokable, HasContext } from '@glint/template/-private/integration';
2
2
  import { Equal, Extends, IsAny, IsNever, IsUnknown } from 'expect-type';
3
3
  interface UnaryExpectations<T> {
4
4
  string: [Equal<T, string>, 'Expected type to be `string`, but got'];
@@ -34,8 +34,8 @@ declare type TypeTestTemplate<T> = abstract new () => HasContext<{
34
34
  element: unknown;
35
35
  }>;
36
36
  declare type ExpectTypeOf = DirectInvokable<{
37
- <T, E extends UnaryExpectationKind>(named: EmptyObject, actual: T, expectation: UnaryExpectation<E>): UnaryExpectations<T>[E] extends [false, infer M] ? UnmetExpectation<M, T> : void;
38
- <T, E extends BinaryExpectationKind, U>(named: EmptyObject, actual: T, expectation: BinaryExpectation<E>, expected: U): BinaryExpectations<T, U>[E] extends [false, infer M] ? UnmetExpectation<M, [T, U]> : void;
37
+ <T, E extends UnaryExpectationKind>(actual: T, expectation: UnaryExpectation<E>): UnaryExpectations<T>[E] extends [false, infer M] ? UnmetExpectation<M, T> : void;
38
+ <T, E extends BinaryExpectationKind, U>(actual: T, expectation: BinaryExpectation<E>, expected: U): BinaryExpectations<T, U>[E] extends [false, infer M] ? UnmetExpectation<M, [T, U]> : void;
39
39
  }>;
40
40
  /**
41
41
  * Given a value, an expectation, and potentially a second value as the
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glint/type-test",
3
- "version": "0.9.7",
3
+ "version": "1.0.0-beta.2",
4
4
  "repository": "typed-ember/glint",
5
5
  "description": "Tools for testing inferred types in Glint-enabled templates",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "expect-type": "^0.15.0"
22
22
  },
23
23
  "peerDependencies": {
24
- "@glint/template": "^0.9.7"
24
+ "@glint/template": "^1.0.0-beta.2"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public"