Excess property checks — only for a fresh object literal¶
Level: 201 · for TypeScript users puzzled by extra-property errors
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: const direct: Point = { x: 1, y: 2, z: 3 } is TS2353, but put the same object in a variable and const later: Point = staged passes: the extra-property check runs only on an object literal written where the type is expected.
Keywords: excess property check, weak type
What the finished page will answer¶
- Why is
{ x: 1, y: 2, z: 3 }an error written directly as aPoint(TS2353) but not when it comes from a variable? - Does the check apply to an object literal passed as an argument or returned from a function?
- What is a weak type, and why does
{ verbos: true }fail against an all-optionalOptionseven from a variable (TS2559)? - How do an index signature, a spread such as
{ ...staged }, oras Pointchange the check? - Which typo does the check exist to catch, given that extra properties are otherwise allowed?
Examples it will need¶
-
excess_property_fresh_literal_tserror.ts— TS2353 for the literal written as a Point and for the literal passed to a function, with no error for the staged object or the spread -
excess_property_weak_type_tserror.ts— TS2559 for a misspelt option assigned from a variable to an all-optional type
See also¶
- Structural typing — the rule this check is an exception to
- Object types — optional properties and index signatures, which change the check
satisfies,asand annotations —satisfies, which applies the same check- Type assertions — the
asthat skips the check entirely - Rust: When a struct refuses ↗ — a Rust struct literal refuses an unknown field every time