-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(type): add scope in setter code to prevent `variable already decl…
…ared` #603 (#606) - When a tuple definition contained "duplicated" union types like [number|null, number|null], the jitted function couldln't be build as some variable like `oldErrors` were already declared. Probably one block per number|null
- Loading branch information
1 parent
5ae467e
commit 9af344f
Showing
2 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { expect, test } from '@jest/globals'; | ||
import { cast, validate } from '@deepkit/type'; | ||
|
||
test('cast literal obj having typed tuple [number | null, number | null] as nested prop', () => { | ||
type MinMax = [number | null, number | null]; | ||
|
||
class T { | ||
building!: { | ||
area: MinMax | ||
}; | ||
} | ||
|
||
// const d = JSON.parse('{"building":{"area":[120,null]}}'); | ||
const d = { | ||
building: { | ||
area: [120, null], | ||
}, | ||
}; | ||
|
||
const errors = validate<T>(d); | ||
expect(errors.length).toBe(0); | ||
|
||
const casted: T = cast<T>(d); | ||
|
||
expect(casted.building.area[0]).toBe(120); | ||
expect(casted.building.area[1]).toBe(null); | ||
}); | ||
|
||
test('cast literal obj to T containing typed tuple', () => { | ||
type SomeData = [string | null, string, string | null]; | ||
|
||
class T { | ||
tuple!: SomeData; | ||
} | ||
|
||
const d = { | ||
tuple: [null, 'z', null], | ||
}; | ||
|
||
const errors = validate<T>(d); | ||
expect(errors.length).toBe(0); | ||
|
||
const casted: T = cast<T>(d); | ||
|
||
expect(casted.tuple[0]).toBe(null); | ||
expect(casted.tuple[1]).toBe('z'); | ||
expect(casted.tuple[2]).toBe(null); | ||
}); | ||
|
||
test("cast literal obj having typed tuple [number | null, number | null] as nested prop", () => { | ||
type MinMax = [min: number | null, max: number | null]; | ||
|
||
class T { | ||
building?: { | ||
area?: MinMax | ||
} | ||
} | ||
|
||
// const d = JSON.parse('{"building":{"area":[120,null]}}'); | ||
|
||
const data: T = cast<T>({ | ||
building: { | ||
area: [120, null] | ||
} | ||
}); | ||
|
||
const errors = validate<T>(data); | ||
expect(errors.length).toBe(0); | ||
}); |