Non è possibile utilizzare decoratori TypeScript perché function.name
è readonly property.
C'è un hacky way:
class ClassName {
// ...
public func = function test() {
}
public func2() {
}
}
let instance = new ClassName();
console.log("RESULT", instance.func['name']);
ma non è esattamente quello che chiedete (cioè nota il prototipo mancante nella dichiarazione di funzione).
Edit: dattiloscritto compilatore non scrivere il nome della funzione, perché non v'è alcun trattamento per SyntaxKind.MethodDeclaration
in emitter.ts:
function shouldEmitFunctionName(node: FunctionLikeDeclaration) {
if (node.kind === SyntaxKind.FunctionExpression) {
// Emit name if one is present
return !!node.name;
}
if (node.kind === SyntaxKind.FunctionDeclaration) {
// Emit name if one is present, or emit generated name in down-level case (for export default case)
return !!node.name || languageVersion < ScriptTarget.ES6;
}
}
Se si vuole sporcarsi le mani, quindi è possibile aggiornare ./node_modules/typescript/lib/typescript.js
file. Basta aggiungere l'ultima condizione:
function shouldEmitFunctionName(node) {
if (node.kind === 173 /* FunctionExpression */) {
// Emit name if one is present
return !!node.name;
}
if (node.kind === 213 /* FunctionDeclaration */) {
// Emit name if one is present, or emit generated name in down-level case (for export default case)
return !!node.name || languageVersion < 2 /* ES6 */;
}
// MODIFIED
if (node.kind === 143 /* MethodDeclaration */) {
return true;
}
}
ed eseguire questo per testare il cambiamento: soluzione
$ node ./node_modules/typescript/lib/typescript.js hello.ts
fonte
2015-11-11 13:22:38
abbastanza intelligente – robsonrosa