microsoft / microsoft/TypeScript
Narrower (literal) type for Function.prototype.name if function is const
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Iniziate esaminando gli esempi della proposta relativi a Function.prototype.name, const expressions e alle chiamate generiche a replaceFunction. Determinate le implicazioni per il sistema di tipi e la compatibilità, quindi definite test che dimostrino che i nomi letterali delle funzioni vengono preservati e che i nomi non validi vengono rifiutati senza modificare il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100