#What to do when types are not exported from npm module

72 messages · Page 1 of 1 (latest)

torpid wagon
#

I have some code:

 await db.transaction(async (trx) => {
  ...
})

inside of that block i need to pass trx to functions, i do so like this:

            const items = await Items.fetchAll({ trx })

sometimes like in this case its totally fine.

static async fetchAll ({ trx = db }): Promise<Array<any>> {
  ...
}

in cases where i type the destructure like so:

static async updateCharacterEquipment ({ character, items, charID, incursionID, trx = db } : { character : Character, items : Array<Item>, charID : number, incursionID : number, trx : any }): Promise<void> {
  ...
}

I get errors:
character/index.character.ts(59,38): error TS7006: Parameter 'trx' implicitly has an 'any' type.
character/index.character.ts(58,46): error TS7031: Binding element 'character' implicitly has an 'any' type.
character/index.character.ts(58,57): error TS7031: Binding element 'items' implicitly has an 'any' type.
character/index.character.ts(58,64): error TS7031: Binding element 'charID' implicitly has an 'any' type.
character/index.character.ts(58,72): error TS7031: Binding element 'incursionID' implicitly has an 'any' type.

Not sure how to handle these errors, and i dont know how to get the Knex.Transaction type so that I can properly type the trx

worn hinge
#

you shouldn't use any to begin with

torpid wagon
#

I dont want to use any

worn hinge
#

you're using it in updateCharacterEquipment

torpid wagon
#

i understand

#

I have to to get the errors to go away

#

because Knex.Transaction, the type of trx, is not exported from the knex module

#

so i have no way to type it

worn hinge
#

the errors are on separate lines but the code you showed seems to all be on the same line?

#

im confused

#

can you make an example in the playground?

torpid wagon
#
    static async updateCharacterEquipment ({ character, items, charID, incursionID, trx = db } : { character : Character, items : Array<Item>, charID : number, incursionID : number, trx : any }): Promise<void> {
        return trx.transaction(async trx => {
            ...
        })
    }

the part where i am returning the trx is the part throwing the error

#

because it doesnt know what trx is

#

lol

worn hinge
#

it's the parameter in the callback that's erroring there

#

there's no type for it

#

that's why it implicitly has an any type there

torpid wagon
#

Right, its of type Knex.Transaction, but I dont have the ability to tell it that

#

which is what id like to do

worn hinge
#

it is exported though

#

you can import it and give it that typing

analog laurelBOT
#
interface Transaction<TRecord extends {} = any, TResult = any[]>
analog laurelBOT
#
export declare namespace knex {
worn hinge
#

it's exported under the namespace

#

wrong line, edited in the message but hte bot preview isn't edited

torpid wagon
#

hmmm

#

i have been trying

#

can you show me how to?

#

i tried importing knex then doing knex.Trasnasction with no luck

worn hinge
#

not confident about importing namespaces, but it should be something like this i believe

import type { Knex } from "knex";
type Transaction = Knex.Transaction; // or consume elsewhere
worn hinge
kindred hawkBOT
#
that_guy977#0

Preview:```ts
import type {Knex} from "knex"
type Transaction<
TRecord extends {} = any,
TResult = any[]

= Knex.Transaction<TRecord, TResult> // or consume elsewhere
// ^?
type X = Transaction["transaction"]
// ^?```

worn hinge
#

all the appropriate types applied

torpid wagon
#

hell yes i had never seen import type { Knex } from "knex"; before importing types

#

is there a way for me to do that in my code, do you have documentation for that?

worn hinge
#

import { Knex } from "knex" also works, this is just being explicit about it being a type

worn hinge
torpid wagon
#

i was doing import Knex from "knex"

worn hinge
#

that accesses the default export, not named exports

torpid wagon
#

i see i see

worn hinge
#
torpid wagon
#

yeah that makse sense for a sec i thought it was some kind of typescript specific import

#

but that makes sense

#

thank you

worn hinge
#

ts adds type imports in the form import type { T, U } or import { type T, value } (and similar alternatives)

#

they're just for clarity

torpid wagon
#

yeah im definitely going to me using more named exports in that fashion

worn hinge
#

or they omit importing the class if the given type is from a class

torpid wagon
#

I see gotcha

worn hinge
torpid wagon
#

thats dope thanks ill read that

#

also 1 more question, do you know how to type objects created with the spread operator like so:

static async addEvent ({ incursionID, eventType, trx = db, ...params } : { incursionID : number, eventType : string, trx: any })

dunno how to type ...params

worn hinge
#

you don't

#

params is just everything else on the object that hasn't been destructured

#

that's how it's typed

#

you haven't specified any extra props that haven't been specified there so params is just {}

torpid wagon
#

when i call it i do pass more stuff in

#

was using it to create a generic object of junk that i pass in based on eventtype

#

await IncursionLog.addEvent({ trx, incursionID, eventType: IncursionLog.EVENT_TYPES.ITEM_PICKUP_SUCCESS, item, equipmentSlot: 'Backpack' })

worn hinge
#

you'd make the function generic over IncursionLog.EVENT_TYPES then

torpid wagon
#

yeeeee that sounds good, ill do that, im translating this to ts from a js so there will be fstuff like that i gota do

worn hinge
#

then you could add types for that to the parameter

#

(why's IncursionLog.EVENT_TYPES in MACRO_CASE btw. that isn't a constant, that looks to be more of an enum
a container for constants is not itself a constant
)

torpid wagon
#

yeah its an enum

worn hinge
#

enums should be PascalCase

torpid wagon
#

my standard was to use caps like that for enums when i was doing JS, now ill probably convert it to a TS enum and pascal it

#

yeah

#

im still just trying to get this thing to compile since the swapover

worn hinge
#

you might want to turn off noImplicitAny and maybe strict for the time being
you aren't going to get type safety for a while so just embrace the lack of it while translating

#

any is commonly used in the process of translation