The strict lint
How the code meets typescript-eslint’s type-checked rules where a rule leaves a choice. CONTRIBUTING says how the lint runs, and when a rule may be turned off for a line.
- A
!gives way to what the compiler can follow: a destructuring or a loop over entries in place of an index, onegetand a check in place ofhasthenget!, a value anifnarrowed held in aconst. Where the code knows what the types cannot (an index within bounds, a key it set, a regex’s group), core’smust(value, 'what')throws naming what is missing, where a!went on with undefined;protocol, which imports nothing, checks with anif. Neverasin its place. In a test, vitest’sassert(x)narrows, and?.will do inside anexpectwhose matcher fails on undefined. - An
asyncfunction with noawaitis a plain one where no caller needs a promise of it, and its callers lose theirawait. Where its type or a caller needs one (a port, a fake of one, a.catchon it), it returnsPromise.resolve(x), andPromise.reject(e)in place of athrow, which a.catchwould miss; a body that may throw on its own goes innew Promise((resolve) => { resolve(work()); }). - What comes from outside (JSON, the wire, a device, a file, the environment, argv, a tool’s output) is
unknown, or a type that says what may be missing, until a schema or a check narrows it, so that a check on it is one the types need too:no-unnecessary-conditionnever takes one away. JSON whose shape the code wrote itself is cast at the call, never by a generic that hides it. Acatchcallback takes(e: unknown), and reads it bye instanceof Error ? e.message : String(e). - A flag a callback sets, which the compiler takes for the value it starts with, is
let done = false as boolean, as typescript-eslint advises for that, with a word on why. ||stays for strings and booleans, where''andfalsefall through as meant; a number or an object takes??, unless0should fall through too, which the line then says.- What is handed around unbound (
stop, a fake’s members) is an arrow or bound. A mock is read in a test as the mock it is, never off its port’s type. - A template holds strings and numbers: an undefined goes by narrowing where it cannot be, and
String(x)keeps the text where it is meant. vitest’s asymmetric matchers are typedany: one inside an object isas unknown. voidin a union isundefined, and a function that answers nothing saysPromise<undefined>, as TypeScript takes noreturnforvoid.
This site counts visits with Cloudflare Web Analytics, without cookies.