Refactor jsdoc types (#16)

* WIP

* Fix type 1

* Missing types info

* Some fixes

* Several types refactoring and fixes

* WIP

* Fix grammar
This commit is contained in:
barsdeveloper
2023-09-22 22:56:33 +02:00
committed by GitHub
parent 78c62ee59a
commit fdd86ce5de
78 changed files with 413 additions and 1010 deletions

View File

@@ -3,23 +3,6 @@ import Configuration from "./Configuration.js"
import MirroredEntity from "./entity/MirroredEntity.js"
import Union from "./entity/Union.js"
/**
* @typedef {import("./Blueprint.js").default} Blueprint
* @typedef {import("./entity/IEntity.js").AnyValue} AnyValue
* @typedef {import("./entity/IEntity.js").AttributeInformation} AttributeInformation
* @typedef {import("./entity/IEntity.js").default} IEntity
* @typedef {import("./entity/IEntity.js").EntityConstructor} EntityConstructor
* @typedef {import("./entity/LinearColorEntity.js").default} LinearColorEntity
*/
/**
* @template {AnyValue} T
* @typedef {import("./entity/IEntity.js").AnyValueConstructor<T>} AnyValueConstructor
*/
/**
* @template T
* @typedef {import("./entity/IEntity.js").TypeGetter<T>} TypeGetter
*/
export default class Utility {
static booleanConverter = {
@@ -233,20 +216,24 @@ export default class Utility {
}
/**
* @param {null | AnyValue | AttributeInformation} value
* @template {AnyValue} T
* @param {T} value
* @returns {SimpleValueType<T>}
*/
static getType(value) {
if (value === null) {
return null
}
if (value?.constructor === Object && /** @type {AttributeInformation} */(value)?.type instanceof Function) {
// @ts-expect-error
return /** @type {AttributeInformation} */(value).type
}
return /** @type {AnyValueConstructor<any>} */(value?.constructor)
return /** @type {SimpleValueType<any>} */(value?.constructor)
}
/**
* @template {new (...args: any) => any} C
* @template {SimpleValue} V
* @template {SimpleValueType<V>} C
* @param {C} type
* @returns {value is InstanceType<C>}
*/
@@ -257,8 +244,8 @@ export default class Utility {
return (acceptNull && value === null) || value instanceof type || value?.constructor === type
}
/** @param {AnyValue} value */
static sanitize(value, targetType = /** @type {AnyValueConstructor<typeof value>} */(value?.constructor)) {
/** @param {AnyValue | Object} value */
static sanitize(value, targetType = /** @type {SimpleValueType<typeof value>} */(value?.constructor)) {
if (targetType instanceof Array) {
targetType = targetType[0]
}
@@ -283,7 +270,7 @@ export default class Utility {
? BigInt(/** @type {Number} */(value))
: new /** @type {EntityConstructor} */(targetType)(value)
}
if (value instanceof Boolean || value instanceof Number || value instanceof String || value instanceof BigInt) {
if (value instanceof Boolean || value instanceof Number || value instanceof String) {
value = value.valueOf() // Get the relative primitive value
}
return value