Skip to content

Narrow type of constructor.nameΒ #52909

Closed
Closed

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.

Activity

  1. MartinJohns commented on Feb 22, 2023

    @MartinJohns
    Contributor

    Duplicate of #37340.

    Trying to narrow via constructor.name is a fairly bad choice. The class may be subclassed, in which case the name will be different, but it will still be a valid instance.

    class Base {}
    class Derived extends Base {}
    
    const base: Base = new Derived();
    console.log(base.constructor.name); // base is typed Base, but logs "Derived"
  2. Zamiell commented on Feb 22, 2023

    @Zamiell
    ContributorAuthor

    Ok. Forgetting constructor.name for a moment, is there a way to go from Foo to the name of the method, purely at compile-time, via some other method? I'm not interested in any run-time behavior, only compile-time.

  3. MartinJohns commented on Feb 22, 2023

    @MartinJohns
    Contributor

    No. See #43325.

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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