Skip to content

Commit

Permalink
Merge pull request #20374 from emberjs/just-SafeString
Browse files Browse the repository at this point in the history
[BUGFIX LTS] Improve SafeString type tests, include in preview types
  • Loading branch information
chriskrycho authored Feb 15, 2023
2 parents 22b3879 + 73c0039 commit bb725ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
25 changes: 12 additions & 13 deletions type-tests/@ember/template-tests.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { SafeString } from '@ember/template/-private/handlebars';
import { htmlSafe, isHTMLSafe } from '@ember/template';
import { htmlSafe, isHTMLSafe, SafeString } from '@ember/template';
import { expectTypeOf } from 'expect-type';

const handlebarsSafeString: SafeString = htmlSafe('lorem ipsum...');
expectTypeOf(htmlSafe('lorem ipsum...')).toEqualTypeOf<SafeString>();
// @ts-expect-error
const regularString: string = htmlSafe('lorem ipsum...');

let trusted = htmlSafe('lorem ipsum...');
expectTypeOf(trusted).toEqualTypeOf<SafeString>();
expectTypeOf(trusted).not.toBeString();
expectTypeOf(isHTMLSafe).guards.toEqualTypeOf<SafeString>();
expectTypeOf(trusted.toHTML()).toBeString();
expectTypeOf(trusted.toString()).toBeString();

function isSafeTest(a: string | SafeString) {
if (isHTMLSafe(a)) {
a = a.toString();
}
expectTypeOf<SafeString>().toMatchTypeOf<{
toString(): string;
toHTML(): string;
}>();

a.toLowerCase();
}
// @ts-expect-error -- we do not allow construction by exporting only the type.
new SafeString('whatever');
1 change: 1 addition & 0 deletions types/preview/@ember/template/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module '@ember/template' {
import { SafeString } from '@ember/template/-private/handlebars';
export type { SafeString };
export function htmlSafe(str: string): SafeString;
export function isHTMLSafe(str: unknown): str is SafeString;
}

0 comments on commit bb725ba

Please sign in to comment.