#are there @functions/classes in typescript? like in java or python

49 messages · Page 1 of 1 (latest)

wide fox
#

?

deft gorge
#

yes

wide fox
#

how do i make one?

deft gorge
#

have you googled it

wide fox
#

google ignores @ in front of keywords so it just googles functions/classes

#

thats why i came here

deft gorge
#

is the @ supposed to be significant

#

do you mean decorators/annotations

wide fox
#

yeah

#

lmao

deft gorge
#

those are not functions nor classes

#

but yes ts has decorators

wide fox
#

actually i just remembered it is @interface in java

#

but yeah, how do i make one

deft gorge
wide fox
#

lmao

#

how does it work in ts

deft gorge
wide fox
#

oh, thats actually surprisingly simple, i didnt know closure functions could be used like this

jolly fiber
#

@wide fox You may want to look at https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#decorators instead of the handbook link - I think the handbook link is describing the legacy TS-specific decorator semantics, rather than the new official decorator spec that is supported as of TS 5.0

TypeScript

Today we’re excited to announce the release of TypeScript 5.0! This release brings many new features, while aiming to make TypeScript smaller, simpler, and faster. We’ve implemented the new decorators standard, added functionality to better support ESM projects in Node and bundlers,

wide fox
#

i'm using ts 4.9 so this doesnt apply to me but thanks

jolly fiber
#

I would probably upgrade and switch over to the official decorators personally.

#

(TS doesn't do semver, 5.0 is no more a breaking change than 4.9)

deft gorge
#

what defines a major version bump for ts?

jolly fiber
#

They just count up to x.9 and then the next one is (x + 1).0

#

4.0 followed 3.9, 5.0 followed 4.9, 6.0 will come after 5.9

wide fox
#

!close

wide fox
#

actually this should be reopened

#

im getting this error

#

here's the route function

jolly fiber
#

!*:screenshot

past forgeBOT
#
Retsam19#2505
`!retsam19:screenshot`:

Rather than screenshots, please provide either code formatted as:

```ts
// code here
```
Or even better, as an example on the Typescript playground https://www.typescriptlang.org/play that is as simple as possible and reproduces the issue. This makes it easier to help you and increases the chances of getting an answer.

wide fox
#
function route(type: "get" | "post", route: string) {
  const router = this.r;
  return function (
    func: any,
    propertyKey: string,
    descriptor: PropertyDescriptor
  ) {
    if (type === "get") {
      return router.get(route, func);
    }
    if (type === "post") {
      return router.post(route, func);
    }
  };
}
#
@route("get", "/auth/login")
  getAuthLogin(req: Request, res: Response) {
    res.json({ status: "ok" });
  }
wide fox
jolly fiber
#

@wide fox It works okay if you turn on the new decorator syntax:

past forgeBOT
#
Retsam19#2505

Preview:```ts
import {Request, Response} from "express"

function route(type: "get" | "post", route: string) {
const router = this.r
return function (
func: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
if (type === "get") {
return router.get(route, fun
...```

wide fox
#

using ts 5.0.4

jolly fiber
#

Yeah, not 100% sure - decorators aren't really my thing.

#

You would want to make sure the runtime code is being transpiled to support ES decorators, too.

#

(Which is relevant if you're using something other than TSC to produce the runtime code)

wide fox
#

transpiling and then running it doesn't change anything i still get this error

#

i figured it out

#

instead of func (which turns out to be just an empty object when logging it in the console) i'm using descriptor.value

#

and it worked

#

thanks for the help