-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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!)
- Loading branch information
1 parent
22b3879
commit 73c0039
Showing
2 changed files
with
13 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |