Skip to content

Rule proposal: Enforce dictionary type style #795

@ark120202

Description

@ark120202

Currently there are few ways to declare a dictionary-like type:

// Key can be only `string` and `number`
type _ = { [key: string]: string };
// Key is any type usable as object key
type _ = { [P in string]: string };
// Same as above
type _ = Record<string, string>;

I think it might make sense to implement it as 2 rules:

  • Disallow object types (or interfaces) with a single index signature member and fix it to mapped type, or enforce the opposite: disallow mapped types of string or number with unused key.
  • Disallow mapped types with unused key and fix it to Record. It also may be extended to enforcing usage of other builtin types, like Partial or Readonly. Not sure if there is a need to do the opposite, since it can be done with ban-types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    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