Preview:```ts
function test(...args: unknown[]) {
console.log(args)
}
//@EnforceFinalMethods
class foo {
@test
bar() {}
}```
You can choose specific lines to embed by selecting them before copying the link.
1 messages · Page 1 of 1 (latest)
Preview:```ts
function test(...args: unknown[]) {
console.log(args)
}
//@EnforceFinalMethods
class foo {
@test
bar() {}
}```
These code generates this
[
λ bar(),
{
kind: "method",
name: "bar",
static: false,
private: false,
access: {
has: λ,
get: λ
},
metadata: undefined,
addInitializer: λ
}
]
It doesn't look like what is described in the method decorator documentation at all
TypeScript Decorators overview
in documents it seems like I need to write things like this:
<T>(
target: Object,
methodName: string | symbol,
propertyDescriptor: TypedPropertyDescriptor<T>
) => TypedPropertyDescriptor<T> | void
I get these stuff by compile my codes in playground and directly execute it in browser console
This is what I got when I click "Go to Definition" in context menu
// lib.es5.d.ts
interface TypedPropertyDescriptor<T> {
enumerable?: boolean;
configurable?: boolean;
writable?: boolean;
value?: T;
get?: () => T;
set?: (value: T) => void;
}
Which is also nothing like I got
I think the most likely reason is that I misunderstood the information provided in the document to some extent.
So where should I look for the correct information?
!helper
Have you enabled experimentalDecorators?
To enable experimental support for decorators, you must enable the experimentalDecorators compiler option either on the command line or in your tsconfig.json:
its been a while since I used decorators, but there used to be a different specification about how decorators would work.
Enabling it, gives a different output in the playground
@obtuse timber There's a note at the top of that page:
NOTE This document refers to an experimental stage 2 decorators implementation. Stage 3 decorator support is available since Typescript 5.0. See: Decorators in Typescript 5.0
This is also a good guide to modern, standard decorators: https://2ality.com/2022/10/javascript-decorators.html
JavaScript decorators have finally reached stage 3! Their latest version is already supported by Babel and will soon be supported by TypeScript. This blog post covers the 2022-03 version (stage 3) of the ECMAScript proposal “Decorators” by Daniel Ehrenberg and Chris Garrett. A decorator is a keyword that starts with an @ symbol and can be pu...
Hmm, should I enable experimentalDecorators if I expect long-term reliable features?
Anyway, now I know what happening, thanks for your help
and
is there something like mdn or other documents for this? I am not a native English speaker and it is a little too hard for me to extract the information I need from a blog
!resolved
No
experimentalDecorators opts in to the old, legacy version of decorators.
okey
I'm not finding any MDN documentation - but it's technically still an "in-progress" language feature that is not supported in browsers yet.
If you want something more technical, https://github.com/tc39/proposal-decorators is the proposal text for it