Utilities for safely and unsafely working around TypeScript type checking.
-
-
Successful conversion
const element: HTMLElement = new Image(); const elementAsImg = trySpecify(element, HTMLImageElement); elementAsImg.src = "image.png";
-
Failed conversion
const element: HTMLElement = new Image(); // The next function call will throw, because the object stored in element // isn't an instance of `HTMLFormElement` const elementAsForm = trySpecify(element, HTMLFormElement); elementAsForm.method = "post";
-
-
// element1 has type HTMLElement const element1 = uncheckedCast<HTMLElement>(document.getElementById("img"));
function next(val: number) { return val + 1; } const str = "hello"; const nextStr = next(uncheckedCast(str));
-
// element1 has type HTMLElement | null const element1 = document.getElementById("img"); // element2 has type HTMLElement const element2 = throwIfNull(document.getElementById("form"));
You can generate the documentation by running the docs
script.
TSDoc is rough around the edges and VS Code won't format correctly when a
@link
tag follows some tags such as @return
. Amoguses have been inserted to
circumvent this problem, and are easy to find and remove when it gets fixed.