fix: Don't forget branding in value type of Record.#106
Open
fix: Don't forget branding in value type of Record.#106
Record.#106Conversation
88bd670 to
78a2882
Compare
Record.
pavadeli
requested changes
Feb 18, 2025
src/types/record.test.ts
Outdated
Comment on lines
219
to
266
| 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>>(); | ||
| }); |
Member
There was a problem hiding this comment.
I suggest we add some more tests. See my suggestion below. Unfortunately, not all of those tests pass right now.
Suggested change
| 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>(); | |
| }); | |
| }); |
src/types/record.ts
Outdated
Comment on lines
103
to
104
| /** 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; |
Member
There was a problem hiding this comment.
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
Contributor
Author
There was a problem hiding this comment.
It's a subtle difference.
- Narrowing:
string -> Branded<string, '...'> - Widening:
Branded<string, '...'> -> string
I called the type Unwidened because it stops the widening case from happening. The problem is that we receive a branded string that accidentally gets widened, not that we received a normal string that we needed to narrow down.
Let me know if you still want me to change the name. I'll move the type to interfaces.ts in any case.
e917f0c to
8fe80ba
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before this fix, branding information was lost design time on the key and value types of a
Recordtype: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.