drullo
June 7, 2022, 12:59pm
1
In the TypeScript course, the code in the Property Decorators section does not work for me. I have confirmed that the MinLength
function does get called and that the return
statement within it also gets called. I’ve placed console.log
statements in these sections and they do get displayed. However, the get()
and set()
code inside of the PropertyDescriptor
never run.
I was able to resolve this by setting useDefineForClassFields
to false, based on the following issues:
opened 11:07AM - 20 Jun 21 UTC
closed 09:42PM - 20 Jun 21 UTC
# Bug Report
### 🔎 Search Terms
* decorator broken
* decorator define… Property
* decorator Object.defineProperty
* decorator target issue
### 🕗 Version & Regression Information
Issue starts in 4.3.x (was previously fine in 4.2.4 and below).
- This changed between versions 4.2.4 and 4.3.x
### ⏯ Playground Link
The playground link with this code works fine, so it's not a reliable test, but when using the NPM version of typescript that is where the issue is, see my gist here:
https://gist.github.com/dannysmc95/da689ae30ef6c05e3fb1778a7c8143a8
If you create a basic file with typescript 4.2.4 you will see the file works, if you then install a 4.3.x version (I did a few versions including latest and next) it will say:
```Cannot read property 'say' of undefined```
### 💻 Code
```typescript
function SetValue(): any {
return (target: Object, propertyKey: string) => {
Object.defineProperty(target, propertyKey, {
get: () => {
return new MyHelper();
},
});
}
}
class MyHelper {
public say(message: string): void {
console.log(message);
}
}
class MyClass {
@SetValue() public helper!: MyHelper;
}
const instance = new MyClass();
console.log(instance.helper.say('hello'));
```
### 🙁 Actual behavior
In versions above 4.2.4 (i.e. 4.3.x) this will throw the error:
```
TypeError: Cannot read property 'say' of undefined
```
In versions 2.4.2 and below it will output:
```
hello
undefined
```
That is correct.
### 🙂 Expected behavior
4.3.x should work the same as 4.2.4.
opened 11:16PM - 04 Jun 21 UTC
closed 12:00AM - 13 Jun 21 UTC
Question
# Bug Report
Using a property decorator doesn't seem to work anymore. In the … playground it does work however (see links below).
### 🔎 Search Terms
typescript 4.3 property decorator
### 🕗 Version & Regression Information
- This changed between versions 4.2 and 4.3
### ⏯ Playground Link
Interestingly this bug doesn't occur in the [playground](https://www.typescriptlang.org/play?noImplicitAny=false&strictNullChecks=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&esModuleInterop=false&declaration=false&emitDecoratorMetadata=false&target=99&jsx=0&allowSyntheticDefaultImports=true#code/GYVwdgxgLglg9mABAEwKYQBSIA4Cc5RwBciAhmAJ4A0iA1qhSQAr7aq5QUDSDiAlIgDeAKESIA8gCMAVuigA6NMBhhULOGw4UseAnBr1qQ0WMQBzVFAwCRp0xAQBnOABtU8l3DNYA5Bag+-ADcJna4liC4SIYhdgC+VMKhiI6WWABupC4gqPzGdmIOYM5uHl6+qQE0mdm5fLGmCSZxwcJxSRAupI6OiADC+YgAAmgQJsBwxClQuCpmIe3CRY5QiBCIALyIqgDu-dYhS-ITcJuIPpKkuD4hy67unt4Qx5N8QA). Maybe it is linked to modules somehow? This is the [link to the repo](https://github.com/maccesch/ts43_decorator_bug).
Only the index.ts and the C1.ts are relevant.
### 💻 Code
The difference seems to be that for this TS code
```typescript
function dec(proto, key) {
Object.defineProperty(proto, key, {
get() { ... }
set(value) { ... }
});
}
class C {
@dec
foo: string;
}
```
In the playground sth like this JS is generated:
```javascript
class C {
}
decorate(C, 'foo', dec);
```
while in my example project this JS is generated:
```javascript
class C {
foo;
}
decorate(C, 'foo', dec);
```
### 🙁 Actual behavior
The code behaves as if the decorator is never executed (it is however).
### 🙂 Expected behavior
The simple field should have been replaced by the property with getter/setter.