Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript] SchemaType.cast (non static) probably has wrong type #9980

Closed
hasezoey opened this issue Mar 1, 2021 · 0 comments
Closed

[Typescript] SchemaType.cast (non static) probably has wrong type #9980

hasezoey opened this issue Mar 1, 2021 · 0 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@hasezoey
Copy link
Collaborator

hasezoey commented Mar 1, 2021

Do you want to request a feature or report a bug?
types

What is the current behavior?
it seems like SchemaType.cast has value "Function" and wants an "Function" to be returned (plainly speaking)

mongoose/index.d.ts

Lines 2472 to 2489 in abf7b02

class SchemaType {
/** SchemaType constructor */
constructor(path: string, options?: any, instance?: string);
/** Get/set the function used to cast arbitrary values to this type. */
// eslint-disable-next-line @typescript-eslint/ban-types
static cast(caster?: Function | boolean): Function;
static checkRequired(checkRequired?: (v: any) => boolean): (v: any) => boolean;
/** Sets a default option for this schema type. */
static set(option: string, value: any): void;
/** Attaches a getter for all instances of this schema type. */
static get(getter: (value: any) => any): void;
/** Get/set the function used to cast arbitrary values to this type. */
cast(caster: (v: any) => any): (v: any) => any;

If the current behavior is a bug, please provide the steps to reproduce.

so things like the following dont work:

class CustomInt extends mongoose.SchemaType {
  constructor(key: string, options: any) {
    super(key, options, 'CustomInt');
  }

  public cast(val) { // Error: Type 'number' is not assignable to type '(v: any) => any'
    return Number(val);
  }
}

and as an example, SchemaNumber has 3 parameters listed, which are not typed:

/**
* Casts to number
*
* @param {Object} value value to cast
* @param {Document} doc document that triggers the casting
* @param {Boolean} init
* @api private
*/
SchemaNumber.prototype.cast = function(value, doc, init) {

What is the expected behavior?

the provided example to not error and have correct types

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
5.11.18


am i just maybe using the wrong type to extend from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants