From 4eaee735644b2a013b542ada4b50351277f38937 Mon Sep 17 00:00:00 2001 From: Omer Sheikh Date: Mon, 29 Aug 2016 01:07:10 +0500 Subject: [PATCH] Add test for invalid property access in unions --- .../unionPropertyExistance.errors.txt | 60 +++++++++++++++++++ .../reference/unionPropertyExistance.js | 44 ++++++++++++++ .../cases/compiler/unionPropertyExistance.ts | 32 ++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tests/baselines/reference/unionPropertyExistance.errors.txt create mode 100644 tests/baselines/reference/unionPropertyExistance.js create mode 100644 tests/cases/compiler/unionPropertyExistance.ts diff --git a/tests/baselines/reference/unionPropertyExistance.errors.txt b/tests/baselines/reference/unionPropertyExistance.errors.txt new file mode 100644 index 00000000000..5dcb6b4d6c3 --- /dev/null +++ b/tests/baselines/reference/unionPropertyExistance.errors.txt @@ -0,0 +1,60 @@ +tests/cases/compiler/unionPropertyExistance.ts(24,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'. + Property 'onlyInB' does not exist on type 'A'. +tests/cases/compiler/unionPropertyExistance.ts(27,5): error TS2339: Property 'notInC' does not exist on type 'ABC'. + Property 'notInC' does not exist on type 'C'. +tests/cases/compiler/unionPropertyExistance.ts(28,4): error TS2339: Property 'notInB' does not exist on type 'AB'. + Property 'notInB' does not exist on type 'B'. +tests/cases/compiler/unionPropertyExistance.ts(29,5): error TS2339: Property 'notInB' does not exist on type 'ABC'. + Property 'notInB' does not exist on type 'B'. +tests/cases/compiler/unionPropertyExistance.ts(32,5): error TS2339: Property 'inNone' does not exist on type 'ABC'. + Property 'inNone' does not exist on type 'A'. + + +==== tests/cases/compiler/unionPropertyExistance.ts (5 errors) ==== + interface A { + inAll: string; + notInB: string; + notInC: string; + } + + interface B { + inAll: boolean; + onlyInB: number; + notInC: string; + } + + interface C { + inAll: number; + notInB: string; + } + + type AB = A | B; + type ABC = C | AB; + + var ab: AB; + var abc: ABC; + + ab.onlyInB; + ~~~~~~~ +!!! error TS2339: Property 'onlyInB' does not exist on type 'AB'. +!!! error TS2339: Property 'onlyInB' does not exist on type 'A'. + + ab.notInC; // Ok + abc.notInC; + ~~~~~~ +!!! error TS2339: Property 'notInC' does not exist on type 'ABC'. +!!! error TS2339: Property 'notInC' does not exist on type 'C'. + ab.notInB; + ~~~~~~ +!!! error TS2339: Property 'notInB' does not exist on type 'AB'. +!!! error TS2339: Property 'notInB' does not exist on type 'B'. + abc.notInB; + ~~~~~~ +!!! error TS2339: Property 'notInB' does not exist on type 'ABC'. +!!! error TS2339: Property 'notInB' does not exist on type 'B'. + + abc.inAll; // Ok + abc.inNone; + ~~~~~~ +!!! error TS2339: Property 'inNone' does not exist on type 'ABC'. +!!! error TS2339: Property 'inNone' does not exist on type 'A'. \ No newline at end of file diff --git a/tests/baselines/reference/unionPropertyExistance.js b/tests/baselines/reference/unionPropertyExistance.js new file mode 100644 index 00000000000..0836032c6db --- /dev/null +++ b/tests/baselines/reference/unionPropertyExistance.js @@ -0,0 +1,44 @@ +//// [unionPropertyExistance.ts] +interface A { + inAll: string; + notInB: string; + notInC: string; +} + +interface B { + inAll: boolean; + onlyInB: number; + notInC: string; +} + +interface C { + inAll: number; + notInB: string; +} + +type AB = A | B; +type ABC = C | AB; + +var ab: AB; +var abc: ABC; + +ab.onlyInB; + +ab.notInC; // Ok +abc.notInC; +ab.notInB; +abc.notInB; + +abc.inAll; // Ok +abc.inNone; + +//// [unionPropertyExistance.js] +var ab; +var abc; +ab.onlyInB; +ab.notInC; // Ok +abc.notInC; +ab.notInB; +abc.notInB; +abc.inAll; // Ok +abc.inNone; diff --git a/tests/cases/compiler/unionPropertyExistance.ts b/tests/cases/compiler/unionPropertyExistance.ts new file mode 100644 index 00000000000..d0bee2fb829 --- /dev/null +++ b/tests/cases/compiler/unionPropertyExistance.ts @@ -0,0 +1,32 @@ +interface A { + inAll: string; + notInB: string; + notInC: string; +} + +interface B { + inAll: boolean; + onlyInB: number; + notInC: string; +} + +interface C { + inAll: number; + notInB: string; +} + +type AB = A | B; +type ABC = C | AB; + +var ab: AB; +var abc: ABC; + +ab.onlyInB; + +ab.notInC; // Ok +abc.notInC; +ab.notInB; +abc.notInB; + +abc.inAll; // Ok +abc.inNone; \ No newline at end of file