-
Notifications
You must be signed in to change notification settings - Fork 13k
Description
Bug Report
π Search Terms
Excess Property Checks
π Version & Regression Information
3.5
β― Playground Link
Playground link with relevant code
π» Code
// Assumption:
// The rules for allowing an object to be assigned to a union type (|) are
// (1) must contain all required properties of at least one union member
// (2) may contain additional properties that belong to any union member
// Examples supporting (but not proving) the assumption
{
type A2 = {a: string; b: string};
type A3 = {a: string; b: boolean; c: boolean};
type A4 = {a: string; b: number; c: number};
type A = A2|A3|A4
// fail because rule (1) not satisfied
const f1:A = {} // error β
const f2:A = {a:''} // error β
const f3:A = {a:'',b:true} // error β
const f4:A = {a:'',b:true,c:1} // error β
// pass because rule (1) satisfied and rule (2) allows excess props of other members
const a1: A = {a: '', b: '', c: true}; // β assignments allow extra props of other union types
const a2: A = {a: '', b: '', c: 1}; // β assignments allow extra props of other union types
}
π Actual documentation
- From Release Notes 3.5
In TypeScript 3.5, the type-checker at least verifies that all the provided properties belong to some union member and have the appropriate type, meaning that the sample above correctly issues an error. Note that partial overlap is still permitted as long as the property types are valid.
π Expected documentation
The actual documentation does not match that observed in the code above. For example, that actual documentation would imply that an empty object could always be assigned to a union of object with required properties, and that is obviously false. So perhaps what the author meant would be given by this slight rewording of the actual documentation:
... the type-checker at least verifies that all the required properties of some (at least one) union member are provided ...
Equivalently -
- The rules for a set of properties to satisfy a union type (|) is
- (1) must contain all required properties of at least one union member
- (2) may contain additional properties that belong to any union member
This expected documentation
- is stronger excess property checking than the actual (current) documentation,
- matches the behavior in the playground code above,
- but is still not the kind of strict excess property checking as discussed in other issues (e.g., Unions without discriminant properties do not perform excess property checkingΒ #20863). Therefore it is not a change of the status quo.
Request to confirm actual intended design behavior.
The above "expected documentation" assumes the intended behavior of the typescript design, based on some supporting evidence (the playground code). However, although that evidence proves that the actual (current) documentation does not match the actual current behavior, it does not prove that the "expected documentation" (above) accurately describes the intended design behavior.
If the intended design behavior does not match that described in "Expected Documentation" then please give an unambiguous description of the intended design behavior.
Downstream development using the typescript API may depend upon knowing what semantics result in a compiler error and what don't. Of course the specs can change (the typescript API documentation warns about that), but having "no hard spec for excess property checking" is different than having "hard excess property checking specs that may change in the future".