Skip to content

Commit bb725ba

Browse files
authored
Merge pull request #20374 from emberjs/just-SafeString
[BUGFIX LTS] Improve SafeString type tests, include in preview types
2 parents 22b3879 + 73c0039 commit bb725ba

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)