Skip to content

Feature request: type-only members #18556

Closed

Description

The following pattern allows one to define run-time structures that also have a compile-time representation:

type FieldMetadata = {}; // omitted;
type Field<T> = {
  instance: T;
  metadata: FieldMetadata;
};
  
export type QuerySpec<D extends {[P in keyof D]: Field<any>}> = {[P in keyof D]: D[P]["instance"]};

However, because Field<T> instances would be effectively singletons, the instance field is never going to be filled in because it's only used for compile-time type checking/inference.

Therefore, I propose to have "type-only" members, e.g.:

readonly inspiration

type Field<T> = {
  typeonly instance: T;
  metadata: FieldMetadata;
};

This would mean that accessing the instance member in an expression context would result in an error (only type indexing is allowed).

member types

type Field<T> = {
  type InstanceType = T;
  metadata: FieldMetadata;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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