Skip to content

Narrow type of constructor.nameΒ #52909

Closed
Closed
@Zamiell

Description

@Zamiell

Suggestion

πŸ” Search Terms

class
constructor.name

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

class Foo {}

type FooName = typeof Foo.constructor.name; // Should be literal `"Foo"`, but is instead `string`

In the context of this issue, I am not sure whether this is a "bug" or a "suggestion", but I went with a suggestion for now.

πŸ“ƒ Motivating Example

I want to use TypeScript to ensure that all of my classes meet some criteria. Consider the following trivial example:

class Foo {}
class Bar {}
class Baz {}

/** We organize them into an array so that we can instantiate them all at the same time with some common parameters. */
const MY_CLASSES = [
  Foo,
  Bar,
  Baz,
] as const;

/** Helper type to retrieve the class names of a tuple containing classes. */
type MyClassNames = InstanceType<typeof MY_CLASSES[number]>["constructor"]["name"]
// Oops! TypeScript messed up, and `MyClassName` is `string` instead of `"Foo" | "Bar" | "Baz"`.

const CLASSES_TO_METADATA = {
  Foo: "something",
  Bar: "something else",
} as const satisfies Record<MyClassNames, string>

Oops, I forgot to put an entry for my Baz class inside of my CLASSES_TO_METADATA object. Even though I carefully took the time to specify a satisfies constraint, TypeScript doesn't care!

I do not think there are any workarounds for this "bug" right now other than to explicitly duplicate the name of the class inside of the class itself, like this:

class Foo {
  readonly name = "Foo" as const;
}
class Bar {
  readonly name = "Bar" as const;
}
class Baz {
  readonly name = "Baz" as const;
}

/** We organize them into an array so that we can instantiate them all at the same time with some common parameters. */
const MY_CLASSES = [
  Foo,
  Bar,
  Baz,
] as const;

/** Helper type to retrieve the class names of a tuple containing classes. */
type MyClassNames = InstanceType<typeof MY_CLASSES[number]>["name"]

const CLASSES_TO_METADATA = {
  Foo: "something",
  Bar: "something else",
} as const satisfies Record<MyClassNames, string>

Very ugly! But in this second code snippet, the satisfies part works properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions