- Error
- Reported by the type checker
TS2386
Overload signatures must all be optional or required.
Overloads of a method or property disagree on whether it is optional. Mark all of them with ? or none of them.
What it means
An overloaded method declares one member. Writing ? on some of its signatures and not on others asks for a member that is both optional and required, which is reported on the signature that differs.
Example
export class Store {
get(key: string): string;
get?(key: number): string;
get(key: string | number): string {
return String(key);
}
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,3): error TS2386: Overload signatures must all be optional or required.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Put
?on every signature of the member, or on none. - If one form really is optional, give it a separate name.