Skip to content
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

Suggestion: A use case requires the "nameof" operator #59063

Closed
6 tasks done
IceCola97 opened this issue Jun 28, 2024 · 3 comments
Closed
6 tasks done

Suggestion: A use case requires the "nameof" operator #59063

IceCola97 opened this issue Jun 28, 2024 · 3 comments

Comments

@IceCola97
Copy link

IceCola97 commented Jun 28, 2024

🔍 Search Terms

"nameof" and "1579"

See also #394 and #1003.

A fix for the magic string problem is a common requirement on the forums. Hoping to see an official proposal for this soon.

(i know the issue #1579 and i check the issue #394 and #1003)

✅ Viability Checklist

⭐ Suggestion

Use "nameof(ISomeInterface)" instead of "ISomeInterface".
Interface is a type, which means we cannot replace "nameof" by declaring a function:

function nameof(type: any): string {
    return "name" in type ? type.name : String(type);
}

...

nameof(ISomeInterface) // error

📃 Motivating Example

This is a specific example to explain why use 'nameof':

function decorator(name: string) {
    return ...;
}

interface IInterface {
    ...
}

// then we can decorate something without a literal string
// @decorator<IInterface>()
@decorator(nameof(IInterface))
class Class {
    ...
}

I hope the typescript can be written this way instead of the following:

function decorator(name: string) {
    return ...;
}

interface IInterface {
    ...
}

// this makes it more prone to errors without type checking
@decorator("IInterface")
class Class {
    ...
}

💻 Use Cases

  1. What do you want to use this for?
    Like the example, for decorate with an interface
  2. What shortcomings exist with current approaches?
    Without type checking, i dont want to write a literal string
  3. What workarounds are you using in the meantime?
    I'm using the literal string, a silly idea i think
@MartinJohns
Copy link
Contributor

This could be implemented without emitting different JS based on the types of the expressions

You mistakingly checked this. Emitting additional JavaScript beyond ECMAScript support is a pretty big No-Go.

@IceCola97
Copy link
Author

This could be implemented without emitting different JS based on the types of the expressions

You mistakingly checked this. Emitting additional JavaScript beyond ECMAScript support is a pretty big No-Go.

Okay, but @decorator(nameof(IInterface)) is also a better way to write it

@MartinJohns
Copy link
Contributor

Okay, but @decorator(nameof(IInterface)) is also a better way to write it

Which would be the exact same issue: emitting different JavaScript depending on the type.

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