From 73c00397f0f464f6f2821a7a89db5fe74a8028b2 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Tue, 14 Feb 2023 16:39:30 -0700 Subject: [PATCH] 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!) --- type-tests/@ember/template-tests.ts | 25 ++++++++++++------------ types/preview/@ember/template/index.d.ts | 1 + 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/type-tests/@ember/template-tests.ts b/type-tests/@ember/template-tests.ts index 7c38e72387e..01f1b419cc4 100644 --- a/type-tests/@ember/template-tests.ts +++ b/type-tests/@ember/template-tests.ts @@ -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(); -// @ts-expect-error -const regularString: string = htmlSafe('lorem ipsum...'); - +let trusted = htmlSafe('lorem ipsum...'); +expectTypeOf(trusted).toEqualTypeOf(); +expectTypeOf(trusted).not.toBeString(); expectTypeOf(isHTMLSafe).guards.toEqualTypeOf(); +expectTypeOf(trusted.toHTML()).toBeString(); +expectTypeOf(trusted.toString()).toBeString(); -function isSafeTest(a: string | SafeString) { - if (isHTMLSafe(a)) { - a = a.toString(); - } +expectTypeOf().toMatchTypeOf<{ + toString(): string; + toHTML(): string; +}>(); - a.toLowerCase(); -} +// @ts-expect-error -- we do not allow construction by exporting only the type. +new SafeString('whatever'); diff --git a/types/preview/@ember/template/index.d.ts b/types/preview/@ember/template/index.d.ts index 32b7d70da6f..be2c034b66e 100644 --- a/types/preview/@ember/template/index.d.ts +++ b/types/preview/@ember/template/index.d.ts @@ -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; }