Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX LTS] Improve SafeString type tests, include in preview types #20374

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}