-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix: Don't forget branding in value type of Record
.
#106
base: next
Are you sure you want to change the base?
Conversation
Every type used to have a `.autoCast` and `.autoCastAll` getter that returns a clone of the type with autocast parsers added. This PR changes that to be separate functions with the same name. This helps with future changes, because maintaining getters that return the `this` types is becoming increasingly hard. BREAKING CHANGE: The getters `.autoCast` and `.autoCastAll` are removed and moved to separately exported functions.
BREAKING CHANGE: The `type`, `input` and `details` properties of `ValidationError` used to be writable, they are now marked as readonly.
801c630
to
a8e3231
Compare
75caf44
to
4040b6e
Compare
4040b6e
to
2888f0f
Compare
88bd670
to
78a2882
Compare
Record
.
test('Branded types', () => { | ||
// Branded values have a particular interaction with the Record type. | ||
type BrandedString = The<typeof BrandedString>; | ||
const BrandedString = string.withBrand('BrandedString'); | ||
|
||
type BrandedKVRecord = The<typeof BrandedKVRecord>; | ||
const BrandedKVRecord = record('BrandedKVRecord', BrandedString, BrandedString); | ||
|
||
// Currently, branded types are not supported as Record key types. They are instead widened to the unbranded base type: | ||
expectTypeOf<BrandedKVRecord>().toEqualTypeOf<Record<string, BrandedString>>(); | ||
// The problem with branded keytypes arises when trying to create a literal of the record type. | ||
expectTypeOf( | ||
// This `.literal()` would give a TS error because the `DeepUnbranding` can't deal with branded key types. | ||
BrandedKVRecord.literal({ | ||
a: 'b', | ||
}), | ||
).toEqualTypeOf<Record<string, BrandedString>>(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we add some more tests. See my suggestion below. Unfortunately, not all of those tests pass right now.
test('Branded types', () => { | |
// Branded values have a particular interaction with the Record type. | |
type BrandedString = The<typeof BrandedString>; | |
const BrandedString = string.withBrand('BrandedString'); | |
type BrandedKVRecord = The<typeof BrandedKVRecord>; | |
const BrandedKVRecord = record('BrandedKVRecord', BrandedString, BrandedString); | |
// Currently, branded types are not supported as Record key types. They are instead widened to the unbranded base type: | |
expectTypeOf<BrandedKVRecord>().toEqualTypeOf<Record<string, BrandedString>>(); | |
// The problem with branded keytypes arises when trying to create a literal of the record type. | |
expectTypeOf( | |
// This `.literal()` would give a TS error because the `DeepUnbranding` can't deal with branded key types. | |
BrandedKVRecord.literal({ | |
a: 'b', | |
}), | |
).toEqualTypeOf<Record<string, BrandedString>>(); | |
}); | |
// Branded values have a particular interaction with the Record type. | |
describe('branded types', () => { | |
test('Branded string', () => { | |
type BrandedString = The<typeof BrandedString>; | |
const BrandedString = string.withBrand('BrandedString'); | |
type BrandedKVRecord = The<typeof BrandedKVRecord>; | |
const BrandedKVRecord = record('BrandedKVRecord', BrandedString, BrandedString); | |
// Currently, branded types are not supported as Record key types. They are instead widened to the unbranded base type: | |
expectTypeOf<BrandedKVRecord>().toEqualTypeOf<Record<string, BrandedString>>(); | |
const branded = BrandedString('branded'); | |
const regular = String('abc'); | |
expectTypeOf({ [regular]: regular }).not.toMatchTypeOf<BrandedKVRecord>(); | |
expectTypeOf({ [regular]: branded }).toEqualTypeOf<BrandedKVRecord>(); | |
expectTypeOf({ [branded]: branded }).toEqualTypeOf<BrandedKVRecord>(); | |
// The problem with branded keytypes arises when trying to create a literal of the record type. We don't apply the `Narrow` utility to | |
// the key type. This `.literal()` would give a TS error because the `DeepUnbranding` can't deal with branded key types. | |
expectTypeOf(BrandedKVRecord.literal({ a: 'b' })).toEqualTypeOf<Record<string, BrandedString>>(); | |
}); | |
test('Branded union', () => { | |
type IntOrUndefined = The<typeof IntOrUndefined>; | |
const IntOrUndefined = int.or(undefinedType); | |
expectTypeOf<IntOrUndefined>().toEqualTypeOf<int | undefined>(); | |
type BrandedKVRecord = The<typeof BrandedKVRecord>; | |
const BrandedKVRecord = record('BrandedKVRecord', int, IntOrUndefined); | |
// Currently, branded types are not supported as Record key types. They are instead widened to the unbranded base type: | |
expectTypeOf<BrandedKVRecord>().toEqualTypeOf<Record<number, int | undefined>>(); | |
}); | |
test('Branded object value', () => { | |
type BrandedObject = The<typeof BrandedObject>; | |
const BrandedObject = object({ a: string }).withBrand('BrandedObject'); | |
type BrandedVRecord = The<typeof BrandedVRecord>; | |
const BrandedVRecord = record('BrandedVRecord', string, BrandedObject.or(literal('whatever'))); | |
expectTypeOf<BrandedVRecord>().toEqualTypeOf<Record<string, BrandedObject | 'whatever'>>(); | |
const unbranded = { a: 'abc' }; | |
const branded = BrandedObject.literal(unbranded); | |
const someString = String('abc'); | |
expectTypeOf({ [someString]: unbranded }).not.toMatchTypeOf<BrandedVRecord>(); | |
expectTypeOf({ [someString]: branded }).toMatchTypeOf<BrandedVRecord>(); | |
}); | |
}); |
/** Small helper type that somehow nudges TS compiler to not widen branded string and number types to their base type. */ | ||
export type Unwidened<T> = T extends T ? T : never; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe something like Narrow
would be a better name than Unwidened
, do you agree? And secondly, please move utility types like this to the interfaces.ts
file
Before this fix, branding information was lost design time on the key and value types of a
Record
type:This same helper type (
Unwidened<T>
) can be used on the key type too, however, this breaks compatibility with.literal()
becauseDeepUnbranded<T>
can't really deal with branded key types yet.