Repository navigation
fix(types): improve branded types - #13990
Conversation
WikiRik
left a comment
There was a problem hiding this comment.
Lot of TS magic happening for my understanding so I'll leave this up for review by @allawesome497
|
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 I fixed it by making the branding code normalize it to const brandedString: NonAttribute<string> | null = null;and making |
|
I've tested this code in a pretty large project and I haven't found any new issue |
wbourne0
left a comment
There was a problem hiding this comment.
Lgtm, my only concern is that this might not handle nested arrays, other than that I think its fine as long as tests pass.
| // 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) |
There was a problem hiding this comment.
should omit be an array of attributes or is it just a string? I'm not certain where this is used.
There was a problem hiding this comment.
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[];
}There was a problem hiding this comment.
Just in case, it's documented here :) https://github.com/sequelize/sequelize/blob/v6/docs/manual/other-topics/typescript.md
|
🎉 This PR is included in version 7.0.0-alpha.6 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Pull Request Checklist
npm run testornpm run test-DIALECTpass with this change (including linting)?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