Skip to content

Commit 73c0039

Browse files
committed
Improve SafeString type tests, include in preview types
Building on the previous, beta-only PR, update the type tests to check for the constructibility of the type, and make sure it is included in the types users can actually consume. (This is the part we will end up backporting to LTS!)
1 parent 22b3879 commit 73c0039

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

type-tests/@ember/template-tests.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { SafeString } from '@ember/template/-private/handlebars';
2-
import { htmlSafe, isHTMLSafe } from '@ember/template';
1+
import { htmlSafe, isHTMLSafe, SafeString } from '@ember/template';
32
import { expectTypeOf } from 'expect-type';
43

5-
const handlebarsSafeString: SafeString = htmlSafe('lorem ipsum...');
6-
expectTypeOf(htmlSafe('lorem ipsum...')).toEqualTypeOf<SafeString>();
7-
// @ts-expect-error
8-
const regularString: string = htmlSafe('lorem ipsum...');
9-
4+
let trusted = htmlSafe('lorem ipsum...');
5+
expectTypeOf(trusted).toEqualTypeOf<SafeString>();
6+
expectTypeOf(trusted).not.toBeString();
107
expectTypeOf(isHTMLSafe).guards.toEqualTypeOf<SafeString>();
8+
expectTypeOf(trusted.toHTML()).toBeString();
9+
expectTypeOf(trusted.toString()).toBeString();
1110

12-
function isSafeTest(a: string | SafeString) {
13-
if (isHTMLSafe(a)) {
14-
a = a.toString();
15-
}
11+
expectTypeOf<SafeString>().toMatchTypeOf<{
12+
toString(): string;
13+
toHTML(): string;
14+
}>();
1615

17-
a.toLowerCase();
18-
}
16+
// @ts-expect-error -- we do not allow construction by exporting only the type.
17+
new SafeString('whatever');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module '@ember/template' {
22
import { SafeString } from '@ember/template/-private/handlebars';
3+
export type { SafeString };
34
export function htmlSafe(str: string): SafeString;
45
export function isHTMLSafe(str: unknown): str is SafeString;
56
}

0 commit comments

Comments
 (0)