Created
May 31, 2022 05:35
-
-
Save ehatricksmith/3034ab66471d992a32ba21c9014d6484 to your computer and use it in GitHub Desktop.
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
type HTMLSafeString = string & Nominal<HTMLSafeString>; | |
function parseHTMLSafeString(maybeSafeString: string): HTMLSafeString { | |
return specialHTMLEscaping(maybeSafeString) as HTMLSafeString; | |
} | |
function Foo({ userEnteredInput }: { userEnteredInput: HTMLSafeString }) { | |
return <div dangerouslySetInnerHTML={{ __html: userEnteredInput }} />; | |
} | |
function App() { | |
return ( | |
<> | |
{/* Type 'string' is not assignable to type 'HTMLSafeString'. */} | |
<Foo userEnteredInput="<script>do-something-nasty</script>" /> | |
{/* OK */} | |
<Foo userEnteredInput={parseHTMLSafeString("<script>do-something-nasty</script>")} /> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment