Skip to content

class and function names should be typed as the actual name #51740

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
rafaeltab opened this issue Dec 3, 2022 · 4 comments
Closed

class and function names should be typed as the actual name #51740

rafaeltab opened this issue Dec 3, 2022 · 4 comments

Comments

@rafaeltab
Copy link

Suggestion

🔍 Search Terms

constructor name, function name

⭐ Suggestion

I would like for the type of a constructor's name property and a functions name property to be the actual name.
Currently, these are both typed as string.

📃 Motivating Example

Current behaviour:

class ExampleClass {}

// type is `string`
type TheNameOfTheClass = (typeof ExampleClass)["name"];

ExampleClass.name; // type is `string`

function exampleFunction() {}

// type is `string`
type TheNameOfTheFunction = (typeof exampleFunction)["name"];

exampleFunction.name; // type is `string`

Proposed behaviour

class ExampleClass {}

// type is `"ExampleClass"`
type TheNameOfTheClass = (typeof ExampleClass)["name"];

ExampleClass.name; // type is `"ExampleClass"`

function exampleFunction() {}

// type is `"exampleFunction"`
type TheNameOfTheFunction = (typeof exampleFunction)["name"];

exampleFunction.name; // type is `"exampleFunction"`

💻 Use Cases

My use case involves an array of entity classes, used by an ORM.
For my unit tests, I want an object with as keys the name of the entity, and as a value, an array containing instances of the same entity.
I expected to be able to do this as follows:

class EntityOne {}
class EntityTwo {}

const entities = [EntityOne, EntityTwo] as const

type TestEntities = {
    [P in (typeof entities)[number] as P["name"]]: InstanceType<P>[]
}
const testEntities: TestEntities = {
  EntityOne: [new EntityOne()],
  EntityTwo : [new EntityTwo()]
}

However, here the type of TestEntities becomes

type TestEntities = {
    [x: string]: EntityOne[] | EntityTwo[];
}

instead of

type TestEntities = {
    "EntityOne": EntityOne[],
    "EntityTwo": EntityTwo[]
}
@Andarist
Copy link
Contributor

Andarist commented Dec 3, 2022

The problem with this is that minifiers are highly popular and they mangle those. So you could easily introduce runtime bugs by relying on those names - both at runtime and type level.

@MartinJohns
Copy link
Contributor

See also: #47203 (comment)

@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 3, 2022

And in case of runtime code, like your const testEntities... you have the issue that derived classes are valid arguments for the base class, but have a different name.

I'd say that code that depends on the name property is very error prone.

@rafaeltab
Copy link
Author

See also: #47203 (comment)

How is it possible for me to spend half an hour going through issues trying to find out if someone asked for this only to have missed it not once or twice but three times?

The problem with this is that minifiers are highly popular and they mangle those. So you could easily introduce runtime bugs by relying on those names - both at runtime and type level.

I see, I guess that makes sense, even if at design time the name is known, it may be changed by minifiers, making them not match.

And in case of runtime code, like your const testEntities... you have the issue that derived classes are valid arguments for the base class, but have a different name.

I'd say that code that depends on the name property is very error prone.

Valid point, I hadn't even thought of that. My code doesn't rely on the name property, I really just want them to have the same name, and ensure I am not missing any entities for my tests.

Do any of you have any alternative methods I could use for this use case?

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

3 participants