#Im having issues with setting up the typescript project
1 messages · Page 1 of 1 (latest)
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
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?
now im getting this error:
const {ArtfightClient } = require("./dist/src/client.js");
^
ReferenceError: require is not defined in ES module scope, you can use import instead
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);
});
}
don't use require in ts, use imports
why do you have src inside dist?
oh you didn't set the root
set rootDir to "./src" in tsconfig to avoid that issue
did that but now it broke my code
wtf it shows me that .js is the extension, but when i click to go to the file it is a .ts file
ts doesn't rewrite the paths, at runtime you'll want to import js
having this issue
add an assert { type: "json" } to the end of the import
import config from '../config.json' asserts {type: "json"};
like this?
assert, not asserts
Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
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?
it was set to true from the beggining
sorry didn't notice
no problem, happens
so does a barrel import work?
nope, same issue
ig ill just use another way to set maxpages in a separate file, what would be the optimal way?
i mean you could just have a config.ts, that'd work
or you could read & parse the json file via fs
yeah if you want to use config.ts you'll also have to transpile that
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
yes, and make it ts
yes
YAY!!!
if you set the main field in package.json you can run node . from the project root
used proper credentials...
that screenshot doesn't really tell me anything
go to the equivalent ts file
oh are you downleveling stuff
nvm, it has a promise later to execute
yeah why is target set so low
wdym
es2022
that could have issues, since node18 doesn't support all es2024 features
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>");
have an interface specifying arguments for each event type, and have on be generic over ClientEvents to receive a callback according to that interface
could u show an example
?
something like this
https://github.com/discordjs/discord.js/blob/main/packages/discord.js/typings/index.d.ts#L5189
https://github.com/discordjs/discord.js/blob/main/packages/discord.js/typings/index.d.ts#L1032 (just the first signature)
export interface ClientEvents {
public on<Event extends keyof ClientEvents>(event: Event, listener: (...args: ClientEvents[Event]) => void): this;
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;
}
yeah
interface IClientEvents{
[ClientEvents.Ready]:[client:ArtfightClient]
}
client.on(ClientEvents.Ready,(client)=>{
console.log("Logged in as "+client.user.username);
})
it works!!!!
!resolved
also see how emit is specified
don't forget the implementation