Skip to content

Narrower (literal) type for Function.prototype.name if function is const #32527

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

Open
5 tasks done
RAnders00 opened this issue Jul 23, 2019 · 1 comment
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@RAnders00
Copy link

Search Terms

ES6 function name, const assertions, as const, function as const

Suggestion

I would like to see the name property of functions to be available at compile time if the given function is a const expression, e.g.:

class Test {
    public a() { console.log("Test.a"); }
    public b() { console.log("Test.b"); }
}

type FunctionWithName<N> = (...args: any[]) => any & { readonly name: N };

function replaceFunction<T, K extends keyof T>(target: T, fn: FunctionWithName<K>) {
    target[fn.name] = fn;
}

// proposed inline syntax
let testInstance = new Test();
replaceFunction(testInstance, function a() { console.log("Replacement.a"); })

// proposed "as const" syntax
const newB = function b() {
    console.log("Replacement.b");
} as const;
replaceFunction(testInstance, newB);

// example of a compile time error since "c" is not assignable to "a" | "b"
replaceFunction(testInstance, function c() { console.log("Replacement.a"); })

Use Cases

My use case is a function like shown above, where I would like to have static type checking available, without having to specify the extra parameter that specifies the function name to replace, e.g. compare:

// current syntax:
replaceFunction(testInstance, "a", function a() { /* ... */ });
// proposed syntax:
replaceFunction(testInstance, function a() { /* ... */ });

Examples

let myObject = {
    a: () => "a",
    b: () => "b"
}

// fictional "mocking" library:
// this syntax can only validate that "a" is in "myObject" through the const "a" parameter
mockingLibrary.mock(myObject, "a", function a() { return "different value" });

// proposed style, using Function.prototype.name
mockingLibrary.mock(myObject, function a() { return "different value" });

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@AnyhowStep
Copy link
Contributor

AnyhowStep commented Jul 23, 2019

I had this idea before, too. But this will not play nicely with minifiers. They'll mangle your function names and your run time and compile time values will be different.

So, it'll work okay for specific cases but not in general.

You can, of course, preserve your function names after minification by disabling the option to mangle function names, or using Object.defineProperty()

Also, in some environments, I think the name is always an empty string, no matter what. I can't remember if my memory is hazy or if it is actually true. I couldn't find anything after a minute of Google. I'm going to assume my memory is bad


Regarding minifiers,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#JavaScript_compressors_and_minifiers

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants