Skip to content

Instantly share code, notes, and snippets.

@ehatricksmith
Created May 31, 2022 05:35
Show Gist options
  • Save ehatricksmith/3034ab66471d992a32ba21c9014d6484 to your computer and use it in GitHub Desktop.
Save ehatricksmith/3034ab66471d992a32ba21c9014d6484 to your computer and use it in GitHub Desktop.
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