Lint rule · ADN0008

adorn/optional-matches-ts

A contracted method's optional parameters are optional to both TypeScript and adorn.

IdADN0008, the prefix of every message
recommendederror, {defaults: false}
stricterror, {defaults: true}
Fixableno
Needs type informationyes: a typed config (parserOptions.projectService)

What it reports

On a contracted method: a parameter TypeScript marks optional (b?: T) with no @Optional(), and a required parameter after an optional one. With {defaults: true} (the strict preset) also a TypeScript default (b = 3) with no @Default().

Why

TypeScript lets a caller omit b?: T, and adorn, which requires every typed position unless it is @Optional(), refuses the omitted argument: the types and the contract disagree. A required parameter after an optional one is refused when the class is installed, since a caller can omit only trailing arguments. A TypeScript default is honoured by enforcement, but no schema, doc or diff can see its value; @Default() publishes it.

Examples

Reported

reported by adorn/optional-matches-ts
import { Contract, NotEmpty } from 'adorn';

@Contract()
class Greeter {
    greet(@NotEmpty() name: string, @NotEmpty() title?: string) {   // TypeScript-optional, adorn-required
        return (title ? title + ' ' : '') + name;
    }
}

Accepted

accepted
import { Contract, NotEmpty, Optional } from 'adorn';

@Contract()
class Greeter {
    greet(@NotEmpty() name: string, @Optional() @NotEmpty() title?: string) {
        return (title ? title + ' ' : '') + name;
    }
}

Details

Under {defaults: true}, write @Default(3) @GreaterThan(0) b = 3: the decorator publishes the value and implies @Optional(). adorn does not check that the two values agree.

To keep a finding on purpose, disable it with a reason, which justify-opt-out checks: // eslint-disable-next-line adorn/optional-matches-ts -- <why>.

Options

OptionTypeDefaultEffect
defaultsbooleanfalseAlso report a TypeScript default with no @Default().

Messages

The text ESLint prints, read from the rule itself. {{…}} is filled in for each finding.

Message idText
tsOptional[ADN0008] '{{name}}' is optional in TypeScript ('?') but not @Optional(): adorn refuses the omitted argument the type allows. Add @Optional().
requiredAfterOptional[ADN0008] '{{name}}' is required but follows the optional '{{prev}}': a caller can omit only trailing arguments, so it is refused at install (DEC-119).
defaultUnpublished[ADN0008] '{{name}}' has a TypeScript default but no @Default(): enforcement honours it (DEC-086), but no schema, doc or diff can see the value. Add @Default(<the same value>).

All rules · the adorn/eslint guide