Skip to content

Disallow objects with extra properties from being assigned to wider typesΒ #62898

@twilkinson3421

Description

@twilkinson3421

πŸ” Search Terms

"extra properties", "narrow erased", "track narrowed"

βœ… Viability Checklist

⭐ Suggestion

Have some way of remembering when a variable could have extra properties not present in its type. Such variables should not be allowed to be assigned to wider types, as this can lead to variables being incorrectly typed.

πŸ“ƒ Motivating Example

The following example results in h being incorrectly typed, yet no error is reported.

type A = { 0: "a"; 1: "b"; 2: "c" };

const n = { 0: "a", 1: "b", 2: "c", length: 3 } as const;

const g: A = n;

type B = { 0: "a"; 1: "b"; 2: "c"; length?: boolean };

const h: B = g;

If we could somehow remember that g might have extra properties that aren't defined in its type, A, then it could be disallowed from being assigned to a wider type (one whose properties are a superset of A), such as B.

πŸ’» Use Cases

  1. What do you want to use this for?
    Having variables be typed correctly
  2. What shortcomings exist with current approaches?
    Variables can be incorrectly typed
  3. What workarounds are you using in the meantime?
    None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions