Skip to content

The documentation of limited excess property checking introduced with ts 3.5 understates actual behaviorΒ #44871

@craigphicks

Description

@craigphicks

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

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

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions