#Im having issues with setting up the typescript project

1 messages · Page 1 of 1 (latest)

stone bane
#

where/when are you getting this error?
are you using node/deno/bun? do you want to use esm or cjs at runtime?

astral shale
#

im getting this error from the compiled to js version of the code

#

i did npx tsc

#

im using node

#

idk what's esm or cjs

stone bane
#

if this is a new project, i'd recommend using esm
set "type": "module" in package.json
and since you're on node, set "module" to "node16" or "nodenext" in tsconfig

#

what's the code that's giving this error?

astral shale
astral shale
# stone bane what's the code that's giving this error?
    login(username_1, password_1) {
        return tslib_1.__awaiter(this, arguments, void 0, function* (username, password, completes = Enumarables_2.Complete.None) {
            yield this.scrapper.login(username, password);
            this.users = new user_1.UserManager(this, new manager_1.Cache());
            this.attacks = new sumbition_1.SubmitionManager(this, new manager_1.Cache(), "attack");
            this.defenses = new sumbition_1.SubmitionManager(this, new manager_1.Cache(), "defense");
            this.characters = new character_1.CharacterManager(this, new manager_1.Cache());
            this.members = new user_1.MemberManager(this, new manager_1.Cache());
            this.user = yield new user_1.ClientUser(this, username).init();
            this.completes = completes;
            this.emit(Enumarables_1.ClientEvents.Ready, this);
        });
    }
stone bane
#

why do you have src inside dist?

#

oh you didn't set the root

#

set rootDir to "./src" in tsconfig to avoid that issue

astral shale
stone bane
#

did you read the errors

#

it tells you what to do

astral shale
#

wtf it shows me that .js is the extension, but when i click to go to the file it is a .ts file

stone bane
#

ts doesn't rewrite the paths, at runtime you'll want to import js

astral shale
#

having this issue

stone bane
#

add an assert { type: "json" } to the end of the import

astral shale
#
import config from '../config.json' asserts {type: "json"};

like this?

stone bane
#

assert, not asserts

astral shale
#

Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.

stone bane
#

wait it's in nodenext but not node16?

#

that's weird

#

what about a barrel import

#

import * as config from '../config.json';

#

and set resolveJsonModule to true in tsconfig

#

wait no

#

just resolveJsonModule should work, without the barrel import?

astral shale
#

it was set to true from the beggining

stone bane
#

sorry didn't notice

astral shale
#

no problem, happens

stone bane
#

so does a barrel import work?

astral shale
#

nope, same issue

#

ig ill just use another way to set maxpages in a separate file, what would be the optimal way?

stone bane
#

i mean you could just have a config.ts, that'd work

#

or you could read & parse the json file via fs

astral shale
#

now this issue:

#

ig ill just move the config file

stone bane
astral shale
#

ok, done, time to test

#

how the hell do i run index.js now

stone bane
#

why do you have an index.js there

#

you should have a ts entry point inside src, you would compile src, then run the entry point from dist

astral shale
#

i dont understand

#

u mean move the index.js file to src ?

stone bane
#

yes, and make it ts

astral shale
#

now how to trigger it?

#

nvm, i know now

#

trigger the js version inside dist right?

stone bane
#

yes

astral shale
#

YAY!!!

stone bane
#

if you set the main field in package.json you can run node . from the project root

astral shale
#

used proper credentials...

stone bane
#

that screenshot doesn't really tell me anything

astral shale
#

there's no way the awaiter could be defined there lol

#

thrown on page.evaluate

stone bane
#

go to the equivalent ts file

astral shale
#

and?

#

it doesnt need to be async, maybe that will fix it

stone bane
#

oh are you downleveling stuff

astral shale
#

nvm, it has a promise later to execute

stone bane
#

yeah why is target set so low

astral shale
#

wdym

stone bane
#

es6 is pretty old

#

what version of node are you using

astral shale
#

node 18.7

#

what else should i use

stone bane
#

es2022

astral shale
#

changed it to ES2024 and it work

#

s

stone bane
#

that could have issues, since node18 doesn't support all es2024 features

astral shale
#

so 2022 is better, ok

#

also i have an issue with event emitter

#

lets say i have a event emitted called Ready, and it emits a client, how do i make it so the parameter in on shows up as of type Client and not any

#
import { ArtfightClient } from "./client.js";
import { ClientEvents } from "./Enumarables.js";
const client = new ArtfightClient();
client.on(ClientEvents.Ready,(client)=>{
    // client shows up as any, i want it to show up as Client
    console.log("Logged in as "+client.user.username);
})
client.login("<username>","<password>");
stone bane
#

have an interface specifying arguments for each event type, and have on be generic over ClientEvents to receive a callback according to that interface

astral shale
#

could u show an example

?

sharp thicketBOT
#
export interface ClientEvents {
public on<Event extends keyof ClientEvents>(event: Event, listener: (...args: ClientEvents[Event]) => void): this;
astral shale
#

so smt like this:

public interface IClientEvents{
Ready: [client:ArtfightClient]
}

class ArtfightClient extends EventEmitter{
on<Event extends keyof IClientEvents>(event:Event, listener: (...args:IClientEvents[Event])=>void):this;
}
stone bane
#

yeah

astral shale
#
interface IClientEvents{
    [ClientEvents.Ready]:[client:ArtfightClient]
}
client.on(ClientEvents.Ready,(client)=>{
    console.log("Logged in as "+client.user.username);
})

it works!!!!

#

!resolved

stone bane
#

also see how emit is specified

stone bane