-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(zui): handle all literal types when transforming to typescript (#377
- Loading branch information
1 parent
72c5668
commit 4f3acd3
Showing
7 changed files
with
108 additions
and
7 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
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
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,52 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { toTypescript } from '.' | ||
import z from '../../z' | ||
|
||
describe('functions', () => { | ||
it('string literals', async () => { | ||
const typings = toTypescript(z.literal('Hello, world!')) | ||
expect(typings).toMatchWithoutFormatting(`'Hello, world!'`) | ||
}) | ||
|
||
it('number literals', async () => { | ||
const code = toTypescript(z.literal(1)) | ||
expect(code).toMatchWithoutFormatting('1') | ||
}) | ||
|
||
it('boolean literals', async () => { | ||
const code = toTypescript(z.literal(true)) | ||
expect(code).toMatchWithoutFormatting('true') | ||
}) | ||
|
||
it('undefined literals', async () => { | ||
const typings = toTypescript(z.literal(undefined)) | ||
expect(typings).toMatchWithoutFormatting('undefined') | ||
}) | ||
|
||
it('null literals', async () => { | ||
const typings = toTypescript(z.literal(null)) | ||
expect(typings).toMatchWithoutFormatting('null') | ||
}) | ||
|
||
it('bigint literals', async () => { | ||
const n = BigInt(100) | ||
const fn = () => toTypescript(z.literal(n)) | ||
expect(fn).toThrowError() | ||
}) | ||
|
||
it('non explicitly discriminated union', async () => { | ||
const schema = z.union([ | ||
z.object({ enabled: z.literal(true), foo: z.string() }), | ||
z.object({ enabled: z.literal(false), bar: z.number() }), | ||
]) | ||
const typings = toTypescript(schema) | ||
expect(typings).toMatchWithoutFormatting(`{ | ||
enabled: true; | ||
foo: string | ||
} | { | ||
enabled: false; | ||
bar: number | ||
} | ||
`) | ||
}) | ||
}) |
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