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 expected number of arguments when using defaultTo() #164

Open
gbcreation opened this issue Feb 21, 2023 · 4 comments
Open

Typescript expected number of arguments when using defaultTo() #164

gbcreation opened this issue Feb 21, 2023 · 4 comments

Comments

@gbcreation
Copy link

Hello,

Given this code sample:

import { Model } from "objectmodel"

const classSchema = {
    id: String,
    lastName: [String],
    firstName: [String]
}

const defaultValues = { lastName: "a" }

class Character extends Model(classSchema).defaultTo(defaultValues) {}

const o = new Character({ id: "1" }) // <- typescript: Expected 0 arguments, but got 1.
console.log(o)

TypeScript underlines { id: "1" } with the error "Expected 0 arguments, but got 1" when creating a new instance of the Character class.

Is there something wrong in this code?

Note that I get the same TypeScript error with the ObjectModel example given at chapter Default values assignment in the doc.

@sylvainpolletvillard
Copy link
Owner

sylvainpolletvillard commented Feb 21, 2023

Nothing wrong with your code. TypeScript definitions have been recently added to the project and they are hard to code and unit test. Thanks for the bug report

@sylvainpolletvillard
Copy link
Owner

I improved the type definitions with defaultTo() in v4.4.4, released just now.
Can you update and confirm that it fiixed your issue ? Thanks

@gbcreation
Copy link
Author

Thank you very much for your quick answer.

v4.4.4 fixes the typescript error for the ObjectModel example in the Default values assignment chapter.

It also fixes it for the code sample in my first post, however I now get another error:

class Character extends Model(classSchema).defaultTo(defaultValues) {}
                     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     //    |_ typescript: Base constructors must all have the same return type

@sylvainpolletvillard
Copy link
Owner

Sounds like a limitation of TypeScript, based on my understanding of this issue: microsoft/TypeScript#40110

I can remove the type error but will lose the ability to return the default value as type when no arguments are passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants