|
| 1 | +import { describe, test } from 'vitest'; |
| 2 | +import { compileSource } from '../../compile.js'; |
| 3 | +import { dedent, expectCompiles, expectCompilationError } from '../testHelpers.js'; |
| 4 | + |
| 5 | +function compile(src) { |
| 6 | + return compileSource(dedent(src), 'test.blop', true); |
| 7 | +} |
| 8 | + |
| 9 | +describe('conditional types', () => { |
| 10 | + test('generic conditional resolves true branch', () => { |
| 11 | + expectCompiles(` |
| 12 | + type IsString<T> = T extends string => true else false |
| 13 | + x: IsString<string> = true |
| 14 | + `); |
| 15 | + }); |
| 16 | + |
| 17 | + test('generic conditional resolves false branch', () => { |
| 18 | + expectCompiles(` |
| 19 | + type IsString<T> = T extends string => true else false |
| 20 | + x: IsString<number> = false |
| 21 | + `); |
| 22 | + }); |
| 23 | + |
| 24 | + test('mismatched branch assignment errors', () => { |
| 25 | + expectCompilationError( |
| 26 | + ` |
| 27 | + type IsString<T> = T extends string => true else false |
| 28 | + x: IsString<number> = true |
| 29 | + `, |
| 30 | + 'Cannot assign true to IsString<number>' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + test('conditional can return never in false branch', () => { |
| 35 | + expectCompiles(` |
| 36 | + type OnlyString<T> = T extends string => T else never |
| 37 | + x: OnlyString<string> = 'ok' |
| 38 | + `); |
| 39 | + }); |
| 40 | + |
| 41 | + test('conditional never false branch rejects values', () => { |
| 42 | + expectCompilationError( |
| 43 | + ` |
| 44 | + type OnlyString<T> = T extends string => T else never |
| 45 | + x: OnlyString<number> = 1 |
| 46 | + `, |
| 47 | + 'Cannot assign 1 to OnlyString<number>' |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + test('conditional can be used inside mapped utility composition', () => { |
| 52 | + const result = compile(` |
| 53 | + type IsString<T> = T extends string => true else false |
| 54 | + x = 1 |
| 55 | + `); |
| 56 | + if (!result.success) { |
| 57 | + throw new Error(`Expected no errors but got: ${JSON.stringify(result.errors)}`); |
| 58 | + } |
| 59 | + }); |
| 60 | +}); |
0 commit comments