Skip to content
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(type): add scope in setter code to prevent `variable already declared #603 #606

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/type/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1792,16 +1792,18 @@ export function handleUnion(type: TypeUnion, state: TemplateState) {
` : '';

state.addCodeForSetter(`
const oldErrors = state.errors;
if (state.errors) state.errors = [];

//type guard for union
if (false) {} ${lines.join(' ')}
else {
${handleErrors}
${state.assignValidationError('type', 'No valid union member found. Valid: ' + stringifyResolvedType(type))}
{
const oldErrors = state.errors;
if (state.errors) state.errors = [];

//type guard for union
if (false) {} ${lines.join(' ')}
else {
${handleErrors}
${state.assignValidationError('type', 'No valid union member found. Valid: ' + stringifyResolvedType(type))}
}
state.errors = oldErrors;
}
state.errors = oldErrors;
`);
}

Expand Down
69 changes: 69 additions & 0 deletions packages/type/tests/issues/tuples.spec.ts
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);
});
Loading