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
/** | |
* Nominal typing for any TypeScript interface or class. | |
* | |
* If T is an enum type, any type which includes this interface | |
* will only match other types that are tagged with the same | |
* enum type. | |
* | |
* See <https://stackoverflow.com/a/37074697/253686> | |
*/ | |
export interface Nominal<T> { |
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
interface User { | |
id: string; | |
name?: string; | |
address?: string; | |
} | |
interface Node { | |
id: string; | |
x?: number; | |
y?: number; |
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 UserId = string & Nominal<UserId>; | |
type NodeId = string & Nominal<NodeId>; | |
interface User { | |
id: UserId; | |
name?: string; | |
address?: string; | |
} | |
interface Node { |
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 'string' is not assignable to type 'UserId'. | |
const user: User = { id: "abcd" }; |
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
function parseUserId(userId: string): UserId { | |
if (!userId.startsWith("__special__prefix")) { | |
throw new Error("This is not a userId!"); | |
} | |
return userId as UserId; | |
} | |
// The happy path | |
const user: User = { id: parseUserId("__special__prefix:abcd") }; |
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 }} />; | |
} | |
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
npx ts-prune -s '__stories__|\\.stories\\.tsx$' |
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
util/__mocks__/dom.tsx:3 - readMetaOrThrow2 | |
components/ProjectViewPage/board/BoardGroupItem/index.tsx:33 - BOARD_OBJECT_CLASS_NAME (used in module) | |
components/ProjectViewPage/board/BoardGroupItem/index.tsx:34 - BOARD_OBJECT_ID_ATTRIBUTE_KEY (used in module) | |
components/ProjectViewPage/insight/TempInsightViewPresentPage/SummaryField.tsx:8 - Props (used in module) |
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
npx ts-prune -s '…patterns here…' | egrep -e '[^)]$' |
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
setTimeout(() => console.log("INTERRUPT"), 10); | |
// Will these logs be interrupted? | |
for (let i = 0; i < 1_000_000; i++) { | |
await (async () => console.log(i))(); | |
} |
OlderNewer