brand-constructor
A brand (Html, SafeUrl, RegexSource, Ascii, SafePath, or one of the project's own, Brand<"Name">) made without its constructor: a cast to it (as Html, as unknown as SafeUrl, <Html>raw, also inside another type, as Html[]), a type argument that names it on a call TypeScript doesn't check it against (inject<SafeUrl>(key), axios.get<Html>(url)), or a type predicate or assertion signature that claims it ((text: string): text is SafeUrl => ...: TypeScript doesn't check a predicate's body; other predicates are no-type-predicate's).
Why: a brand says that a check ran: the HTML went through a sanitizer, the URL can't run script, btoa can encode the text. vue-v-html, vue-unsafe-url and unhandled-failure accept a value of the type without looking further, and TypeScript refuses a plain string where one is asked for. A cast gives the type without the check, so the next rule trusts a value nobody checked. A generic that only says what it returns does the same: nothing it is given decides the type.
✕ Rejectedbrand-constructor
ts
import type { Html } from "@evobug_com/safe-ts-std/html";
import type { SafeUrl } from "@evobug_com/safe-ts-std/url";
declare function readCache<T>(key: string): T;
export function avatar(url: string): SafeUrl {
return url as SafeUrl;
}
export const banner = readCache<Html>("banner");✅ Accepted: the constructors
✓ Accepted0 diagnostics
ts
import { escapeHtml, type Html } from "@evobug_com/safe-ts-std/html";
import { safeHref, type SafeUrl } from "@evobug_com/safe-ts-std/url";
declare function readCache(key: string): string;
export function avatar(url: string): SafeUrl {
return safeHref(url);
}
export const banner: Html = escapeHtml(readCache("banner"));| Brand | Made by |
|---|---|
Html | escapeHtml(text), or a sanitizer declared with defineSanitizer((raw) => DOMPurify.sanitize(raw)) |
SafeUrl | safeHref(url), or safeUrl(url) once it isn't an Err |
RegexSource | escapeRegExp(text) |
Ascii | isAscii(text) (narrows text), asciiOf(n) |
SafePath | pathUnder(root, ...parts) once it isn't an Err |
Brand<"Name"> | the functions declared with brandConstructor("Name", make) (brands of your own) |
A brand of the project's own is held the same way, also where it is declared in another file than the cast (text as UserId, trust<UserId>(text): trust vouches for any type, so it is refused for a brand, whose constructor is the one place that vouches for it):
✕ Rejectedbrand-constructor
ts
import { type Brand, brandConstructor } from "@evobug_com/safe-ts-std/brand";
import { err, type Result, trust } from "@evobug_com/safe-ts-std/result";
export type UserId = Brand<"UserId">;
export const parseUserId = brandConstructor("UserId", (text: string): Result<string, { text: string }> =>
/^u_[0-9a-z]{12}$/.test(text) ? text : err({ text }),
);
export function fromSession(text: string): UserId {
return trust<UserId>(text);
}When the check is one the checker can't see (a URL your router builds from its own route table, HTML your server rendered from a template), make the brand in one module marked // @safe-ts std and listed under "trustedFiles" in package.json (unlisted-marker). The rules are off there, the cast is reviewed with the module, and safe-ts check names the file in its "escape hatches" line, like an unsafe():
✓ Accepted0 diagnostics
ts
// @safe-ts std
// URLs the router builds from its route table: they start with `/`.
import type { SafeUrl } from "@evobug_com/safe-ts-std/url";
export function routeUrl(path: `/${string}`): SafeUrl {
return path as SafeUrl;
}For one cast, a // safe-ts-ignore brand-constructor: <reason> comment on the line before works too (Ignoring one finding), and is counted with the escape hatches as well. Prefer the module when several places make the same brand: the reason is then written once, next to the code that makes it true.
✓ Accepted0 diagnostics
ts
import type { Html } from "@evobug_com/safe-ts-std/html";
export function footer(rendered: string): Html {
// safe-ts-ignore brand-constructor: the server renders the footer from a template, escaping on
return rendered as Html;
}Type arguments are accepted where TypeScript checks them against the arguments, so they can't make a brand from nothing: Vue's ref, shallowRef, computed, reactive, defineProps, defineModel, withDefaults, React's useState, useMemo, useRef, the built-in Map, Set, Array, Promise, and std's err, match, unwrapOr, unsafe. Anywhere else, leave the type argument out: TypeScript then infers it from what the function is given, and refuses unless that is a brand already. The validators refuse a brand in their type themselves (json-type): data can't vouch for itself.
What the rule can't see: any. A value typed any is assignable to a brand without a cast (no-any reports the any). A generic function of your own that casts inside (return value as T) is reported there, by no-unchecked-cast. The brands are known by where they are imported from (@evobug_com/safe-ts-std/html, /url, /regexp, /ascii, /path, /brand, and the same from @evobug_com/safe-ts-vue), so a type of your own named Html isn't one; a brand of your own is a type alias of Brand<"Name">, through aliases of the same file (one written as another alias of an imported brand is followed in the file it is written in only).