#ts ๐Ÿ‘€

112 messages ยท Page 1 of 1 (latest)

paper spoke

Hey, I'm using jsdoc with some of my functions. Is it possible to type a mongodb document using it? Since if I import the model it just isn't 100% correct

warm hareBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
paper spoke

Well, "type"

This is my model

const mongoose = require("mongoose");

const schema = new mongoose.Schema({
    guildId: {
        type: String, unique: true
    },
    userId: {
        type: String
    },
    level: {
        type: Number,
        default: 0
    },
    exp: {
        type: Number,
        default: 0
    },
    weeklyExp: {
        type: Number,
        default: 0
    },
});
module.exports = mongoose.model("levelling", schema);

Was gonna use it in something like this


    /**
     * @param {GuildMember} member 
     * @param {TextChannel} channel 
     * @param {type} settings 
     */
    calculateXptoAdd(member, channel, settings = []) {
 //code
    }

What I'm gonna pass will look something like this:

[
    { type: 'role', id: '1045819599955963906', amount: '5' },
    { type: 'channel', id: '1092045790270197770', amount: '1' },
    { type: 'role', id: '977826521165205524', amount: '100' }
  ]
brazen hill

That schema and your variable you pass have no resemblance, so how did you expect that to work?

paper spoke

I was just giving that for context ๐Ÿ˜…

Probably wasnโ€™t necessary

I honestly wanna move to ts at this rate but it will be painful to rewrite my bot in it when I donโ€™t know much of it yet

brazen hill

You wonโ€™t have to rewrite it fully. First step is just installing typescript, tsc init to get a tsconfig and add allowJs: true. Then you can start gradually renaming to .ts and adding types to parameters (and then fixing any issues your js code had but you never knew about because you didnโ€™t have ts)

paper spoke

How will that work? Like do I have to compile everything?

Since I bet you canโ€™t really type with jsdoc like I want to

brazen hill

You can

But ts is better

paper spoke

As always ๐Ÿ˜‚

How will working with ts and js work

brazen hill

djs is typed with JSDoc after all

brazen hill
paper spoke
paper spoke
brazen hill
brazen hill

Unless you use deno or bun to run ts directly.

paper spoke

so node . Will work or wonโ€™t?

paper spoke
brazen hill

Can also use tsc --watch to have it transpile on every file save

paper spoke

Ah, is that where people have like a dist folder of just the fully compiled code and run that

brazen hill

Yes. Thatโ€™s what tsc does for you

paper spoke

Ahh gotcha

Then to run itโ€™ll just be node ./dist/index.js

If Iโ€™m in the main folder

brazen hill

Best way is to put ./dist/index.js in your package.json as main, then itโ€˜ll just be node .

paper spoke

Ahhhhhhh

I didnโ€™t know you could do that ๐Ÿ’€

Alright thanks

Iโ€™ll clone my main bot folder on my laptop and give it a go later or tomorrow or whatever

The hard thing will be to learn how to type mongo documents but the site has a guide on how to use ts with mongoose

brazen hill

Afaik mongoose is typed so you wouldnโ€™t even need to do anything once you use it in typescript itโ€˜ll just work

paper spoke

Idk if yoy need to or not but I think you need to add stuff into the thing

Idk the words

Err

Nvm

paper spoke

ts ๐Ÿ‘€

knotty urchin
brazen hill Afaik mongoose is typed so you wouldnโ€™t even need to do anything once you use it...

Its typed yes , but the schema if you do it in TS directly it will not look like the one in js
example in ts :

import type { blacklistUsers } from '#type/index.js';
import { model, Schema } from 'mongoose';

const blacklist = new Schema<blacklistUsers>({
    guildID: { type: String },
    ID: { type: String },
    reason: { type: String },
});
export default model<blacklistUsers>('userBlacklist', blacklist);

// The following stuff is in another file 

export interface blacklistUsers {
    guildID: string;
    ID: string;
    reason?: string;
}
paper spoke

Yh I saw this ๐Ÿ˜…, why would you do it in another file though?

Or do you not have to

As they also say to extent the interface

err

import { HydratedDocument } from 'mongoose';

interface IUser {
  name: string;
  email: string;
  avatar?: string;
}

const user: HydratedDocument<IUser> = new User({
  name: 'Bill',
  email: 'bill@initech.com',
  avatar: 'https://i.imgur.com/dM7Thhn.png'
});
knotty urchin
paper spoke

fair

paper spoke

or is it not neccesary to use this HydratedDocument thing

knotty urchin

i used mongoose with TS so , i know a bit XD

i never used that form of writing tho const user: HydratedDocument<IUser> = new User({}) , i used it like i sent it earlier

//blacklist.ts
import type { blacklistUsers } from '#type/index.js'; //(Interface import)
import { model, Schema } from 'mongoose';

const blacklist = new Schema<blacklistUsers>({
    guildID: { type: String },
    ID: { type: String },
    reason: { type: String },
});
export default model<blacklistUsers>('userBlacklist', blacklist);

// ------------------- database.d.ts

export interface blacklistUsers {
    guildID: string;
    ID: string;
    reason?: string;
}

// ------------------- blacklist slash command
import blacklistUser from '#database/blacklistUser.js'; //(You import your schema made earlier)
...
await blacklistUser.create({
        guildID: interaction.guild.id,
        ID: interaction.user.id,
        reason: reason,
    });
vague dew

๐Ÿ‡

paper spoke
knotty urchin

you can yea

paper spoke

idek what the hydrated thing does you see

knotty urchin

as i can see i think it replace the .create({})

i just think, im not sure about that

paper spoke

why do i get this error even though it's literally the correct path

vague dew

Did you module.exports (export default (for TS)) correctly

paper spoke

AH

fuck this is gonna be fun

im so stupid

LMAO

vague dew

(Recommendation) You should use ts-node instead of having a dist since its faster and you dont need to replace .ts in filenames with .js every time

paper spoke

i just compile everything

vague dew

I'll be honest I used to rm -rf my ./dist then tsc ๐Ÿ’€

paper spoke

it's what Qjuh said to do

vague dew

Is it efficient though?

It takes like a few seconds

paper spoke

yh takes a few secs

idm that

knotty urchin
paper spoke

yh that's my non compiled stuff, probably should have shown the other one lol

i9 was right

was still using module.exports ๐Ÿ’€๐Ÿคฃ

knotty urchin

Oh , yeah it doesnt help xD

vague dew
paper spoke

๐Ÿ˜ญ

jovial kernel
vague dew

I'll probably shorten it some way or another

jovial kernel

no you do tsc watch

vague dew

What does that do?

paper spoke

I've always wondered

With ts

How do you make like, a new class or something for Events

So you can do something like (probs bad ts all together thouggh ๐Ÿ’€)

export new Event('interactionCreate) {}

type of thing

jovial kernel

remember all js code is valid ts

vague dew

And all ts code is valid js ||when compiled||

paper spoke

yh but i actually dk how to do that type of thing with js ๐Ÿ’€

i just think it looks neat

So you can do something like (probs bad ts all together thouggh ๐Ÿ’€)

export new Event('interactionCreate', async (some params) => { 
    //code
}) 
vague dew

What the hell is export new Event

Where did default go, lol

paper spoke

missed default

vague dew

Alright

vague dew
knotty urchin

Dont need default if theres more than 1 thing in the module tho if i recall that correctly

vague dew

I do it for safety ngl

jovial kernel

u need to give it a name

paper spoke

Thanks for helping people ๐Ÿ™