microsoft / microsoft/TypeScript
The class is accessible from the field decorator expression when Babel and the Proposal Playground throw a ReferenceError
@rbuckton arbeitet bereits daran.
Seit 01.2.2024.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"decorator", "ReferenceError", "cannot access", "before initialization"
🕗 Version & Regression Information
- Tested versions: 5.3.3 and Nightly
⏯ Playground Link
💻 Code
function RegisterClass(
xClass,
_context
) {
console.log('Registered class ' + xClass);
}
function RegisterField(xClass) {
return function InnerRegisterField(
_value,
_context
) {
console.log(
`Registered field ${_context.name.toString()} in class ${xClass.name}`
);
};
}
// This is a showcase of the difference (and probably a bug) between the decorators
// compilation of TypeScript and Babel/javascriptdecorators.org.
//
// When the class decorator @RegisterClass is applied,
// all the field decorators @RegisterField(Article) evaluate without errors and
// the class is accessible from the decorator expression.
// But if you comment out the @RegisterClass then at the runtime there is an error "Cannot access 'Article' before initialization".
// Babel (https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=GYVwdgxgLglg9mABAJQKYHMYGcqoE4DCANgIZZYAUAUIogB7FlYA0NiA-hArnVFQJSIA3my5gscIqgB0ROOgoByNJhz5UAE0QRS5RIsQBqeo3L8A3FQC-VKqEiwEKDNlx4AYjFRENFBrqxBEVo8VCgQPCR7aHgkAEkwMHwVV3xPb182WnYANxIiEFRWWmyxHj5aIKztBAkpWXlqEpKAAxS1UK1gLx9EABIhTm5UXmkwEgBbGSg4AGUoPBgwBX4rRCXtAP6hfyYxydQrFuqLNitLGyoAAXa3UywqHSZEAEE8WB1UYTYblw70nwUN4fKT8NiwKBSSw_W5pHq-YEwT5g2hlVBgKDQ1G1BYgaBwPAUDRVZoAeQARgArVDQaRMGDoMAUKAAC2wzEQWgAPlzhFZTrQbDYgA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2&prettier=false&targets=&version=7.23.9&externalPlugins=&assumptions=%7B%7D)
// and the Decorators Proposal Playground (https://javascriptdecorators.org/) throw a ReferenceError in both cases,
// no mater you apply the class decorator or not.
@RegisterClass
class Article {
@RegisterField(Article)
title;
@RegisterField(Article)
content;
constructor(d) {
Object.assign(this, d || {});
}
}
🙁 Actual behavior
A ReferenceError is thrown only when you do not apply the class decorator, otherwise the class is accessible. That differs from Babel and https://javascriptdecorators.org/
🙂 Expected behavior
There should be a ReferenceError when trying to access the class from the field decorator expression, no matter if you have class decorator applied or not.
Additional information about the issue
This is a showcase of the difference (and probably a bug) between the decorators
compilation of TypeScript and Babel/javascriptdecorators.org.
When the class decorator @RegisterClass is applied,
all the field decorators @RegisterField(Article) evaluate without errors,
the class Article is accessible from the decorator expression.
But if you comment out the @RegisterClass then occurs an error "Cannot access 'Article' before initialization".
Babel (Link with code: https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=GYVwdgxgLglg9mABAJQKYHMYGcqoE4DCANgIZZYAUAUIogB7FlYA0NiA-hArnVFQJSIA3my5gscIqgB0ROOgoByNJhz5UAE0QRS5RIsQBqeo3L8A3FQC-VKqEiwEKDNlx4AYjFRENFBrqxBEVo8VCgQPCR7aHgkAEkwMHwVV3xPb182WnYANxIiEFRWWmyxHj5aIKztBAkpWXlqEpKAAxS1UK1gLx9EABIhTm5UXmkwEgBbGSg4AGUoPBgwBX4rRCXtAP6hfyYxydQrFuqLNitLGyoAAXa3UywqHSZEAEE8WB1UYTYblw70nwUN4fKT8NiwKBSSw_W5pHq-YEwT5g2hlVBgKDQ1G1BYgaBwPAUDRVZoAeQARgArVDQaRMGDoMAUKAAC2wzEQWgAPlzhFZTrQbDYgA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2&prettier=false&targets=&version=7.23.9&externalPlugins=&assumptions=%7B%7D)
and the Decorators Proposal Playground (https://javascriptdecorators.org/) throw a ReferenceError in both cases,
no mater the class decorator is applied or not.
This looks like a bug and leads to confusion, because the code you wrote once worked, but suddenly throws a ReferenceErrors only because you decided to removed the class decorator. And also i think that TypeScript should throw a compile time error when we try to access the class when it's not initialized.
Thanks a lot! Hope i provided enough details.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.