Skip to content

Narrow type of constructor.name #52909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
Zamiell opened this issue Feb 22, 2023 · 3 comments
Closed
5 tasks done

Narrow type of constructor.name #52909

Zamiell opened this issue Feb 22, 2023 · 3 comments

Comments

@Zamiell
Copy link
Contributor

Zamiell commented Feb 22, 2023

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.

@MartinJohns
Copy link
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"

@Zamiell
Copy link
Contributor Author

Zamiell commented Feb 22, 2023

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.

@MartinJohns
Copy link
Contributor

No. See #43325.

@Zamiell Zamiell closed this as completed Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants