Skip to content

Use discriminated unions to "branch" types for Property Names #1345

@bradzacher

Description

@bradzacher

Types for things like ClassProperty/MethodDefinition/Property, etc are pretty ugly.

If the property name is computed, then any Expression is valid, otherwise only Identifier | Literal is valid.

However we don't represent this in the types, we just have a single object with key: Expression.

It'll be a bit of extra work, but we can improve upon this by doing something like:

type PropertyNameNonComputed = Identifier | Literal;
type PropertyNameComputed = Expression;

interface ClassPropertyBase extends BaseNode {
  type: AST_NODE_TYPES.ClassProperty;
  key: PropertyNameNonComputed | PropertyNameComputed;
  computed: boolean;
  // ... other props
}

interface ClassPropertyNonComputedKey extends ClassPropertyBase {
  key: PropertyNameNonComputed;
  computed: false;
}

interface ClassPropertyComputedKey extends ClassPropertyBase {
  key: PropertyNameComputed;
  computed: true;
}

type ClassProperty = ClassPropertyNonComputedKey | ClassPropertyComputedKey;

Nodes we should do this for:

  • ClassProperty
  • TSAbstractClassProperty
  • MethodDefinition
  • TSAbstractMethodDefinition
  • Property
  • TSPropertySignature
  • TSMethodSignature
  • MemberExpression
  • OptionalMemberExpression

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpackage: typescript-estreeIssues related to @typescript-eslint/typescript-estree

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy