Skip to content

no-optional-field ​

An optional property (key?: T) in a type the project declares: write key: Option<T>. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json (or check --idiomatic). Reported by safe-ts check only: whether a type is a JSON payload or a component's props depends on where it is used, anywhere in the program, which the editor and the lint plugin, one file at a time, don't see. A // safe-ts-ignore no-optional-field: <reason> is judged by safe-ts check too: the editor and the lint plugin don't call it unused.

Why: extra properties are allowed on any value that isn't an object literal, and an optional property accepts a value without the key. Together they let a value of any type through, with no cast:

ts
type Base = { readonly id: string };
type WithTag = { readonly id: string; readonly tag?: string };

const raw = { id: "a", tag: 42 };
const base: Base = raw; // fine: extra properties are allowed on a non-literal
const tagged: WithTag = base; // fine: `tag` is optional, and `Base` doesn't have it
tagged.tag?.toUpperCase(); // TypeError: 42 has no toUpperCase

tag: Option<string> closes it: a value without the key isn't assignable, so the key is written where the object is made (tag: undefined when there is none) and checked there.

✅ Accepted: the key is always there, and may be undefined

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

export type Profile = { readonly id: string; readonly nickname: Option<string> };

export function guest(id: string): Profile {
  return { id, nickname: undefined };
}

❌ Rejected (not checked here: the rule needs the whole program)

ts
export type Profile = {
  readonly id: string;
  readonly nickname?: string; // no-optional-field
};

key?: Option<T> is reported too: it is still optional. prefer-option is the neighbouring rule for T | undefined; with both on, key?: T | undefined is reported by each, and key: Option<T> satisfies both.

Not reported ​

Optional is right where the shape is someone else's, or a framework's rules apply:

  • Optional parameters (f(x?: T)), optional tuple elements ([string, number?]) and methods: not properties. (An optional method with parameters is no-method-signature's; written as a property, readonly seek?: (offset: number) => void, it is reported here. One without parameters, close?(): void, is reported by neither.)
  • JSON types: a type given to parseJson<T>, validate<T>, fromFields<T> or arbitrary<T> anywhere in the program, every type it names, an inline type in the call, and a field typed with JsonNull, JsonValue or RawJson. There key?: says the payload may leave the key out (or send null), and the generated validator checks that (JSON, Absence).
  • Framework props: the type argument of Vue's defineProps<T>() (also in withDefaults), and in .tsx / .jsx files the first parameter of a component (a function whose name starts with a capital letter, also inside memo(...) / forwardRef(...)) or the P of FC<P>, with the types they extend. A prop's absence is the framework's to handle (withDefaults, a default in the destructuring).
  • A library's options: a type built on a package's or the platform's type (interface Options extends RequestInit, AxiosRequestConfig & { retry?: number }, also through Partial, Readonly, Pick, Omit, Required). The library reads its keys by presence.
  • Declarations: .d.ts files, ambient blocks (declare module "x", declare global, declare namespace) and ambient declarations (declare function, declare const, declare class): they describe types defined elsewhere.
  • Decorated class fields (@Prop() size?: number): a framework fills them.

What the rule can't see ​

  • Partial<T> and mapped types with ? ({ [K in keyof T]?: T[K] }): every key optional, the same hole, but not written as key?:.
  • A props or JSON type used only through another name the checker doesn't follow (type Props = ComponentProps<typeof Card>), or declared in a namespace.
  • A global type the project declares (declare global { interface AppConfig {} }) used as a base: taken for the platform's, so a type extending it isn't reported.

On an adopter's codebase about 1,250 optional properties split into 437 in JSON types, 86 in declarations and 25 in library options, and about 700 reported, most of them options bags of the project's own functions and view models: that is why the rule is opt-in.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra