Skip to content

fix(types): improve branded types - #13990

Merged
ephys merged 10 commits into
mainfrom
ephys/ts-fixes
Feb 2, 2022
Merged

ephys merged 10 commits into
mainfrom
ephys/ts-fixes

Conversation

@ephys

@ephys ephys commented Jan 22, 2022 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • [n/a] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • [n/a] Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

I noticed a few instances where the current branded type implementation (NonAttribute & CreationOptional) did not work as expected.

I've updated the tests to include them and reworked the implementation of both branded types.

They should be a lot more stable, I even removed the array special case as it wasn't needed anymore.

Bonus: These types should be a lot more readable too

@ephys ephys self-assigned this Jan 22, 2022
@ephys ephys added the typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Jan 22, 2022
@ephys
ephys requested review from WikiRik and wbourne0 January 22, 2022 14:59
@ephys ephys mentioned this pull request Jan 26, 2022
3 of 6 tasks

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lot of TS magic happening for my understanding so I'll leave this up for review by @allawesome497

@ephys

ephys commented Jan 27, 2022 •

Copy link
Copy Markdown
Member Author

Just noticed a new (fixed, now) bug: It was not possible to assign null to branded fields

// this failed
const brandedString: NonAttribute<string | null> = null;

because null & { [Brand]?: true } never works.

I fixed it by making the branding code normalize it to

const brandedString: NonAttribute<string> | null = null;

and making IsBranded remove null/undefined values before checking for the property

@ephys ephys mentioned this pull request Jan 28, 2022
4 of 6 tasks
@ephys

ephys commented Jan 29, 2022

Copy link
Copy Markdown
Member Author

I've tested this code in a pretty large project and I haven't found any new issue

wbourne0
wbourne0 previously approved these changes Feb 2, 2022

@wbourne0 wbourne0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lgtm, my only concern is that this might not handle nested arrays, other than that I think its fine as long as tests pass.

Comment thread types/lib/model.d.ts
// fields branded with NonAttribute are excluded
: IsBranded<M[Key], typeof NonAttributeBrand> extends true ? never
// check 'omit' option is provided & exclude those listed in it
: Options['omit'] extends string ? (Key extends Options['omit'] ? never : Key)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should omit be an array of attributes or is it just a string? I'm not certain where this is used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a type but meant to be string or union of strings
It's used as an escape hatch if it turns out there is an issue with branded types and the user needs another way to omit types, instead of NonAttribute<T>.

It's used like this:

class User extends Model<InferAttributes<User, { omit: 'projects' | 'groups' }>> {
  id: number;
  name: string;
  projects?: Project[];
  groups?: Group[];
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread types/test/infer-attributes.ts Resolved
@ephys
ephys merged commit 940060a into main Feb 2, 2022
@ephys
ephys deleted the ephys/ts-fixes branch February 2, 2022 11:52
ephys added a commit that referenced this pull request Feb 6, 2022
@ephys ephys mentioned this pull request Feb 6, 2022
ephys added a commit that referenced this pull request Feb 6, 2022
ephys added a commit that referenced this pull request Feb 6, 2022
@github-actions

github-actions Bot commented Feb 7, 2022

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 7.0.0-alpha.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

aliatsis pushed a commit to creditiq/sequelize that referenced this pull request Jun 2, 2022
vanthome pushed a commit to vanthome/sequelize that referenced this pull request Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released on @v7 typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants