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

Create index.d.ts #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Create index.d.ts #28

wants to merge 3 commits into from

Conversation

CoveMB
Copy link

@CoveMB CoveMB commented Sep 8, 2020

Adding typescript types
Types where missing :)

Adding typescript types
index.d.ts Outdated
type Constructor<T = {}> = new (...args: any[]) => T;

export default function (options?: {
identifiers?: string[];
Copy link

@kratos-42 kratos-42 Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this line, we are validating if any of the provided properties (identifiers or fields) are empty, so maybe you should define both properties as required instead of optional (since they cannot be empty anyway).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, I made options, identifiers and fields required.

index.d.ts Outdated

export default function (options?: {
identifiers?: string[];
fields?: string[];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can read in the README example, the compound fields can be specified as an Array. So the type for the fields property should be Array<string | string[]>.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should I read more, I changed it to accept also an array of strings

index.d.ts Outdated
export default function (options?: {
identifiers?: string[];
fields?: string[];
}): <T extends typeof Model>(model: T) => T & Constructor<T>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking in account the compatibility with other objection plugins, the return type should be just the provided model.

Example: https://github.com/oscaroox/objection-timestamps/blob/master/src/index.ts#L25.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed objection timestamp implementation to return the model itself.
Thanks for all the feedback!

- Made Options, identifiers and fields required
- Extended types accepted by identifiers and fields to allow for string or array of strings
- Changed the returned value to be the model itself and not a Constructor of it
@CoveMB
Copy link
Author

CoveMB commented Sep 30, 2020

Hello, any update on this ? :)

index.d.ts Outdated
import { Model } from 'objection';

export default function (options: {
identifiers: Array<string | string[]>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is odd. identifiers is not technically required, since it defaults to ["id"]:

  options = Object.assign({
    identifiers: ['id']
  }, options);

Otherwise looks good, works fine.

Copy link

@kratos-42 kratos-42 Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right. If you don't set the options.identifiers, the validation I talked before won't fail because it has the default value ['id']. It just will fail in case you set identifiers as [] or not set the fields option. So you can change identifiers to be optional, but keep the fields as required, mb :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I made the identifiers optional

Made identifier optional
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

Successfully merging this pull request may close these issues.

3 participants