UNPKG

jsii-diff

Version:

Assembly comparison for jsii

81 lines (76 loc) 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); jest.setTimeout(50000); const PREAMBLE = ` export interface IFoo { readonly foo: string; } export interface IBar { readonly bar: string; } export interface IBaz { readonly baz: string; } export interface ICombined extends IFoo, IBar { } `; describe.each(['argument', 'prop'])('%s position', (where) => { function sources(old, noo) { return [old, noo].map((type) => { switch (where) { case 'argument': return ` ${PREAMBLE} export class Api { public static fooAndBar(x: ${type}) { void(x); return ''; } }`; case 'prop': return ` ${PREAMBLE} export interface Props { readonly input: ${type}; } export class Api { public static fooAndBar(props: Props) { Array.isArray(props); return ''; } }`; } }); } // ---------------------------------------------------------------------- test('types can remain unchanged', () => (0, util_1.expectNoError)(...sources('IFoo & IBar', 'IFoo & IBar'))); // ---------------------------------------------------------------------- test('can turn interface into intersection', () => (0, util_1.expectNoError)(...sources('ICombined', 'IFoo & IBar'))); // ---------------------------------------------------------------------- test('can not turn interface into intersection that requires a non-extended type', () => (0, util_1.expectError)(/testpkg.ICombined does not extend testpkg.IBaz/, ...sources('ICombined', 'IFoo & IBaz'))); // ---------------------------------------------------------------------- test('can turn part of union into intersection', () => (0, util_1.expectNoError)(...sources('string | ICombined', 'string | (IFoo & IBar)'))); // ---------------------------------------------------------------------- test('can require fewer intersection elements', () => (0, util_1.expectNoError)(...sources('IFoo & IBar & IBaz', 'IFoo & IBar'))); // ---------------------------------------------------------------------- test('can not add intersection elements', () => (0, util_1.expectError)(/some of testpkg.IBar & testpkg.IFoo are not assignable to testpkg.IBaz/, ...sources('IFoo & IBar', 'IFoo & IBar & IBaz'))); }); // ---------------------------------------------------------------------- test('allow requiring a new interface in intersection if interface is extended at the same time', () => (0, util_1.expectNoError)(` ${PREAMBLE} export interface IChangeable extends IFoo {} export class Api { public static fooAndBar(x: IChangeable) { void(x); return ''; } }`, ` ${PREAMBLE} export interface IChangeable extends IFoo, IBar {} export class Api { public static fooAndBar(x: IFoo & IBar) { void(x); return ''; } }`)); //# sourceMappingURL=type-intersections.test.js.map