altmp / altv-types

Type definitions for the alt:V JavaScript modules.
https://www.npmjs.com/~vadzz
MIT License
31 stars 74 forks source link

Improvement of meta methods #129

Closed xxshady closed 2 years ago

xxshady commented 2 years ago

Now we need to pass generic parameter into meta methods every time, and there is no autocomplete for the meta keys in current solution, so its not very convenient.

I added empty interfaces for global meta, player, entity (currently) to be extended by users, which works just like the event functions. (e.g. alt.on with alt.IClientEvent)

Example usage

import * as alt from "alt-shared"

declare module "alt-shared" {
  export interface ICustomGlobalMeta {
    numberMeta: number
    stringMeta: string
    123: any
  }
}

const value /* number | undefined */ = alt.getMeta("numberMeta") // return type from ICustomGlobalMeta['numberMeta']
const unknown /* unknown */ = alt.getMeta("unknown")

// key: string
alt.deleteMeta("anyKey")
alt.hasMeta("anyKey")

// autocomplete keys: 'numberMeta' | 'stringMeta' from ICustomGlobalMeta
alt.deleteMeta("stringMeta")
alt.hasMeta("numberMeta")

alt.setMeta("stringMeta", "value") // ok stringMeta: string in ICustomGlobalMeta
alt.setMeta("stringMeta", 123) // error: value must be a string

alt.setMeta("anyKey", undefined) // ok value type is unknown
alt.setMeta(123, "anything") // error: key must be a string

// these overloads remains only for backward compatibility and are marked as deprecated
const genericValue /* null | undefined */ = alt.getMeta<null>("deprecated")
alt.setMeta<number>("deprecated", 123) // ok value: number
alt.setMeta<number>("deprecated", "str") // error: value must be a number
xxshady commented 2 years ago

What do you think about adding a different interface for each type of meta and each entity class? e.g. IVehicleSyncedMeta for vehicle synced meta (vehicle.setSyncedMeta)

xxshady commented 2 years ago

Currently stuck on fixing type checking in alt.setMeta when value type param does not match the type in ICustomGlobalMeta

import * as alt from "alt-shared"

enum CustomGlobalMeta {
  Example = "example",
  Example2 = "example2",
}

declare module "alt-shared" {
  export interface ICustomGlobalMeta {
    [CustomGlobalMeta.Example]: number
    [CustomGlobalMeta.Example2]: string
  }
}

// 1nd overload setMeta(key: CustomGlobalMeta, value: number)
alt.setMeta(CustomGlobalMeta.Example, 123) // ok
// 2nd overload setMeta(key: string, value: unknown)
alt.setMeta(CustomGlobalMeta.Example, "awdd") // type check is ignored here
xxshady commented 2 years ago

@C0kkie, can you check to see if it's too messed up?

C0kkie commented 2 years ago

Is this still a draf or

xxshady commented 2 years ago

@C0kkie yes its still a draft because some interfaces and overloads havent been added yet, i just want to know if its worth it so i can add the rest

xxshady commented 2 years ago

I didn't add meta interfaces for some classes (worldobject, some child colshape classes, child blip classes) because I think they are unnecessary