-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jsii-diff): correctly handle assignability of type unions (#500)
- Loading branch information
Showing
3 changed files
with
161 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { Test } from 'nodeunit'; | ||
import { expectError, expectNoError } from './util'; | ||
|
||
export = { | ||
|
||
// ---------------------------------------------------------------------- | ||
|
||
async 'type unions in return structs can be the same'(test: Test) { | ||
await expectNoError(test, | ||
` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
returnHenk(): Henk { return { henk: 'henk' }; } | ||
} | ||
`, ` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
returnHenk(): Henk { return { henk: 'henk' }; } | ||
} | ||
`); | ||
|
||
test.done(); | ||
}, | ||
|
||
// ---------------------------------------------------------------------- | ||
|
||
async 'type unions in return structs can be narrowed'(test: Test) { | ||
await expectNoError(test, | ||
` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
returnHenk(): Henk { return { henk: 'henk' }; } | ||
} | ||
`, ` | ||
export interface Henk { | ||
readonly henk: string; | ||
} | ||
export class Actions { | ||
returnHenk(): Henk { return { henk: 'henk' }; } | ||
} | ||
`); | ||
|
||
test.done(); | ||
}, | ||
|
||
// ---------------------------------------------------------------------- | ||
|
||
async 'type unions in return structs can not be widened'(test: Test) { | ||
await expectError(test, | ||
/some of string \| number \| boolean are not assignable to string \| number/, | ||
` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
returnHenk(): Henk { return { henk: 'henk' }; } | ||
} | ||
`, ` | ||
export interface Henk { | ||
readonly henk: string | number | boolean; | ||
} | ||
export class Actions { | ||
returnHenk(): Henk { return { henk: 'henk' }; } | ||
} | ||
`); | ||
|
||
test.done(); | ||
}, | ||
|
||
// ---------------------------------------------------------------------- | ||
|
||
async 'type unions in input structs can be the same'(test: Test) { | ||
await expectNoError(test, | ||
` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
takeHenk(_henk: Henk): void { } | ||
} | ||
`, ` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
takeHenk(_henk: Henk): void { } | ||
} | ||
`); | ||
|
||
test.done(); | ||
}, | ||
|
||
// ---------------------------------------------------------------------- | ||
|
||
async 'type unions in input structs can be widened'(test: Test) { | ||
await expectNoError(test, | ||
` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
takeHenk(_henk: Henk): void { } | ||
} | ||
`, ` | ||
export interface Henk { | ||
readonly henk: string | number | boolean; | ||
} | ||
export class Actions { | ||
takeHenk(_henk: Henk): void { } | ||
} | ||
`); | ||
|
||
test.done(); | ||
}, | ||
|
||
// ---------------------------------------------------------------------- | ||
|
||
async 'type unions in input structs can not be narrowed'(test: Test) { | ||
await expectError(test, | ||
/string \| number is not assignable to string/, | ||
` | ||
export interface Henk { | ||
readonly henk: string | number; | ||
} | ||
export class Actions { | ||
takeHenk(_henk: Henk): void { } | ||
} | ||
`, ` | ||
export interface Henk { | ||
readonly henk: string; | ||
} | ||
export class Actions { | ||
takeHenk(_henk: Henk): void { } | ||
} | ||
`); | ||
|
||
test.done(); | ||
}, | ||
}; |