Merge branch 'typeguardsByConstructorSigniture' of https://github.com/vvakame/TypeScript into vvakame-typeguardsByConstructorSigniture

This commit is contained in:
Mohamed Hegazy
2015-05-06 12:44:45 -07:00
4 changed files with 713 additions and 9 deletions

View File

@@ -3634,6 +3634,10 @@ module ts {
return type;
}
function getUnionTypeOfSubtypeConstituents(source: UnionType, target: Type): Type {
return getUnionType(filter(source.types, t => isTypeSubtypeOf(t, target)));
}
function getReducedTypeOfUnionType(type: UnionType): Type {
// If union type was created without subtype reduction, perform the deferred reduction now
if (!type.reducedType) {
@@ -5386,17 +5390,35 @@ module ts {
}
// Target type is type of prototype property
let prototypeProperty = getPropertyOfType(rightType, "prototype");
if (!prototypeProperty) {
return type;
if (prototypeProperty) {
let targetType = getTypeOfSymbol(prototypeProperty);
if (targetType !== anyType) {
// Narrow to the target type if it's a subtype of the current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
}
// If the current type is a union type, remove all constituents that aren't subtypes of the target.
if (type.flags & TypeFlags.Union) {
return getUnionTypeOfSubtypeConstituents(<UnionType>type, targetType);
}
}
}
let targetType = getTypeOfSymbol(prototypeProperty);
// Narrow to target type if it is a subtype of current type
if (isTypeSubtypeOf(targetType, type)) {
return targetType;
// Target type is type of construct signature
let constructSignatures: Signature[];
if (rightType.flags & TypeFlags.Interface) {
constructSignatures = resolveDeclaredMembers(<InterfaceType>rightType).declaredConstructSignatures;
}
// If current type is a union type, remove all constituents that aren't subtypes of target type
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
else if (rightType.flags & TypeFlags.Anonymous) {
constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);
}
if (constructSignatures && constructSignatures.length !== 0) {
let instanceType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
// Pickup type from union types
if (type.flags & TypeFlags.Union) {
return getUnionTypeOfSubtypeConstituents(<UnionType>type, instanceType);
}
return instanceType;
}
return type;
}

View File

@@ -0,0 +1,220 @@
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(12,10): error TS2339: Property 'bar' does not exist on type 'A'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(33,5): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(34,10): error TS2339: Property 'bar' does not exist on type 'B<number>'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(63,10): error TS2339: Property 'bar2' does not exist on type 'C1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(82,10): error TS2339: Property 'bar' does not exist on type 'D'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(109,10): error TS2339: Property 'bar2' does not exist on type 'E1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(131,11): error TS2339: Property 'foo' does not exist on type 'string | F'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(132,11): error TS2339: Property 'bar' does not exist on type 'string | F'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(157,11): error TS2339: Property 'foo2' does not exist on type 'G1'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(179,11): error TS2339: Property 'bar' does not exist on type 'H'.
==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (10 errors) ====
interface AConstructor {
new (): A;
}
interface A {
foo: string;
}
declare var A: AConstructor;
var obj1: A | string;
if (obj1 instanceof A) { // narrowed to A.
obj1.foo;
obj1.bar;
~~~
!!! error TS2339: Property 'bar' does not exist on type 'A'.
}
var obj2: any;
if (obj2 instanceof A) { // can't narrow type from 'any'
obj2.foo;
obj2.bar;
}
// a construct signature with generics
interface BConstructor {
new <T>(): B<T>;
}
interface B<T> {
foo: T;
}
declare var B: BConstructor;
var obj3: B<number> | string;
if (obj3 instanceof B) { // narrowed to B<number>.
obj3.foo = 1;
obj3.foo = "str";
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
obj3.bar = "str";
~~~
!!! error TS2339: Property 'bar' does not exist on type 'B<number>'.
}
var obj4: any;
if (obj4 instanceof B) { // can't narrow type from 'any'
obj4.foo = "str";
obj4.foo = 1;
obj4.bar = "str";
}
// has multiple construct signature
interface CConstructor {
new (value: string): C1;
new (value: number): C2;
}
interface C1 {
foo: string;
bar1: number;
}
interface C2 {
foo: string;
bar2: number;
}
declare var C: CConstructor;
var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
obj5.foo;
obj5.bar1;
obj5.bar2;
~~~~
!!! error TS2339: Property 'bar2' does not exist on type 'C1'.
}
var obj6: any;
if (obj6 instanceof C) { // can't narrow type from 'any'
obj6.foo;
obj6.bar1;
obj6.bar2;
}
// with object type literal
interface D {
foo: string;
}
declare var D: { new (): D; };
var obj7: D | string;
if (obj7 instanceof D) { // narrowed to D.
obj7.foo;
obj7.bar;
~~~
!!! error TS2339: Property 'bar' does not exist on type 'D'.
}
var obj8: any;
if (obj8 instanceof D) { // can't narrow type from 'any'
obj8.foo;
obj8.bar;
}
// a construct signature that returns a union type
interface EConstructor {
new (): E1 | E2;
}
interface E1 {
foo: string;
bar1: number;
}
interface E2 {
foo: string;
bar2: number;
}
declare var E: EConstructor;
var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1.
obj9.foo;
obj9.bar1;
obj9.bar2;
~~~~
!!! error TS2339: Property 'bar2' does not exist on type 'E1'.
}
var obj10: any;
if (obj10 instanceof E) { // can't narrow type from 'any'
obj10.foo;
obj10.bar1;
obj10.bar2;
}
// a construct signature that returns any
interface FConstructor {
new (): any;
}
interface F {
foo: string;
bar: number;
}
declare var F: FConstructor;
var obj11: F | string;
if (obj11 instanceof F) { // can't type narrowing, construct signature returns any.
obj11.foo;
~~~
!!! error TS2339: Property 'foo' does not exist on type 'string | F'.
obj11.bar;
~~~
!!! error TS2339: Property 'bar' does not exist on type 'string | F'.
}
var obj12: any;
if (obj12 instanceof F) { // can't narrow type from 'any'
obj12.foo;
obj12.bar;
}
// a type with a prototype, it overrides the construct signature
interface GConstructor {
prototype: G1; // high priority
new (): G2; // low priority
}
interface G1 {
foo1: number;
}
interface G2 {
foo2: boolean;
}
declare var G: GConstructor;
var obj13: G1 | G2;
if (obj13 instanceof G) { // narrowed to G1. G1 is return type of prototype property.
obj13.foo1;
obj13.foo2;
~~~~
!!! error TS2339: Property 'foo2' does not exist on type 'G1'.
}
var obj14: any;
if (obj14 instanceof G) { // can't narrow type from 'any'
obj14.foo1;
obj14.foo2;
}
// a type with a prototype that has any type
interface HConstructor {
prototype: any; // high priority, but any type is ignored. interface has implicit `prototype: any`.
new (): H; // low priority
}
interface H {
foo: number;
}
declare var H: HConstructor;
var obj15: H | string;
if (obj15 instanceof H) { // narrowed to H.
obj15.foo;
obj15.bar;
~~~
!!! error TS2339: Property 'bar' does not exist on type 'H'.
}
var obj16: any;
if (obj16 instanceof H) { // can't narrow type from 'any'
obj16.foo1;
obj16.foo2;
}

View File

@@ -0,0 +1,276 @@
//// [typeGuardsWithInstanceOfByConstructorSignature.ts]
interface AConstructor {
new (): A;
}
interface A {
foo: string;
}
declare var A: AConstructor;
var obj1: A | string;
if (obj1 instanceof A) { // narrowed to A.
obj1.foo;
obj1.bar;
}
var obj2: any;
if (obj2 instanceof A) { // can't narrow type from 'any'
obj2.foo;
obj2.bar;
}
// a construct signature with generics
interface BConstructor {
new <T>(): B<T>;
}
interface B<T> {
foo: T;
}
declare var B: BConstructor;
var obj3: B<number> | string;
if (obj3 instanceof B) { // narrowed to B<number>.
obj3.foo = 1;
obj3.foo = "str";
obj3.bar = "str";
}
var obj4: any;
if (obj4 instanceof B) { // can't narrow type from 'any'
obj4.foo = "str";
obj4.foo = 1;
obj4.bar = "str";
}
// has multiple construct signature
interface CConstructor {
new (value: string): C1;
new (value: number): C2;
}
interface C1 {
foo: string;
bar1: number;
}
interface C2 {
foo: string;
bar2: number;
}
declare var C: CConstructor;
var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
obj5.foo;
obj5.bar1;
obj5.bar2;
}
var obj6: any;
if (obj6 instanceof C) { // can't narrow type from 'any'
obj6.foo;
obj6.bar1;
obj6.bar2;
}
// with object type literal
interface D {
foo: string;
}
declare var D: { new (): D; };
var obj7: D | string;
if (obj7 instanceof D) { // narrowed to D.
obj7.foo;
obj7.bar;
}
var obj8: any;
if (obj8 instanceof D) { // can't narrow type from 'any'
obj8.foo;
obj8.bar;
}
// a construct signature that returns a union type
interface EConstructor {
new (): E1 | E2;
}
interface E1 {
foo: string;
bar1: number;
}
interface E2 {
foo: string;
bar2: number;
}
declare var E: EConstructor;
var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1.
obj9.foo;
obj9.bar1;
obj9.bar2;
}
var obj10: any;
if (obj10 instanceof E) { // can't narrow type from 'any'
obj10.foo;
obj10.bar1;
obj10.bar2;
}
// a construct signature that returns any
interface FConstructor {
new (): any;
}
interface F {
foo: string;
bar: number;
}
declare var F: FConstructor;
var obj11: F | string;
if (obj11 instanceof F) { // can't type narrowing, construct signature returns any.
obj11.foo;
obj11.bar;
}
var obj12: any;
if (obj12 instanceof F) { // can't narrow type from 'any'
obj12.foo;
obj12.bar;
}
// a type with a prototype, it overrides the construct signature
interface GConstructor {
prototype: G1; // high priority
new (): G2; // low priority
}
interface G1 {
foo1: number;
}
interface G2 {
foo2: boolean;
}
declare var G: GConstructor;
var obj13: G1 | G2;
if (obj13 instanceof G) { // narrowed to G1. G1 is return type of prototype property.
obj13.foo1;
obj13.foo2;
}
var obj14: any;
if (obj14 instanceof G) { // can't narrow type from 'any'
obj14.foo1;
obj14.foo2;
}
// a type with a prototype that has any type
interface HConstructor {
prototype: any; // high priority, but any type is ignored. interface has implicit `prototype: any`.
new (): H; // low priority
}
interface H {
foo: number;
}
declare var H: HConstructor;
var obj15: H | string;
if (obj15 instanceof H) { // narrowed to H.
obj15.foo;
obj15.bar;
}
var obj16: any;
if (obj16 instanceof H) { // can't narrow type from 'any'
obj16.foo1;
obj16.foo2;
}
//// [typeGuardsWithInstanceOfByConstructorSignature.js]
var obj1;
if (obj1 instanceof A) {
obj1.foo;
obj1.bar;
}
var obj2;
if (obj2 instanceof A) {
obj2.foo;
obj2.bar;
}
var obj3;
if (obj3 instanceof B) {
obj3.foo = 1;
obj3.foo = "str";
obj3.bar = "str";
}
var obj4;
if (obj4 instanceof B) {
obj4.foo = "str";
obj4.foo = 1;
obj4.bar = "str";
}
var obj5;
if (obj5 instanceof C) {
obj5.foo;
obj5.bar1;
obj5.bar2;
}
var obj6;
if (obj6 instanceof C) {
obj6.foo;
obj6.bar1;
obj6.bar2;
}
var obj7;
if (obj7 instanceof D) {
obj7.foo;
obj7.bar;
}
var obj8;
if (obj8 instanceof D) {
obj8.foo;
obj8.bar;
}
var obj9;
if (obj9 instanceof E) {
obj9.foo;
obj9.bar1;
obj9.bar2;
}
var obj10;
if (obj10 instanceof E) {
obj10.foo;
obj10.bar1;
obj10.bar2;
}
var obj11;
if (obj11 instanceof F) {
obj11.foo;
obj11.bar;
}
var obj12;
if (obj12 instanceof F) {
obj12.foo;
obj12.bar;
}
var obj13;
if (obj13 instanceof G) {
obj13.foo1;
obj13.foo2;
}
var obj14;
if (obj14 instanceof G) {
obj14.foo1;
obj14.foo2;
}
var obj15;
if (obj15 instanceof H) {
obj15.foo;
obj15.bar;
}
var obj16;
if (obj16 instanceof H) {
obj16.foo1;
obj16.foo2;
}

View File

@@ -0,0 +1,186 @@
interface AConstructor {
new (): A;
}
interface A {
foo: string;
}
declare var A: AConstructor;
var obj1: A | string;
if (obj1 instanceof A) { // narrowed to A.
obj1.foo;
obj1.bar;
}
var obj2: any;
if (obj2 instanceof A) { // can't narrow type from 'any'
obj2.foo;
obj2.bar;
}
// a construct signature with generics
interface BConstructor {
new <T>(): B<T>;
}
interface B<T> {
foo: T;
}
declare var B: BConstructor;
var obj3: B<number> | string;
if (obj3 instanceof B) { // narrowed to B<number>.
obj3.foo = 1;
obj3.foo = "str";
obj3.bar = "str";
}
var obj4: any;
if (obj4 instanceof B) { // can't narrow type from 'any'
obj4.foo = "str";
obj4.foo = 1;
obj4.bar = "str";
}
// has multiple construct signature
interface CConstructor {
new (value: string): C1;
new (value: number): C2;
}
interface C1 {
foo: string;
bar1: number;
}
interface C2 {
foo: string;
bar2: number;
}
declare var C: CConstructor;
var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
obj5.foo;
obj5.bar1;
obj5.bar2;
}
var obj6: any;
if (obj6 instanceof C) { // can't narrow type from 'any'
obj6.foo;
obj6.bar1;
obj6.bar2;
}
// with object type literal
interface D {
foo: string;
}
declare var D: { new (): D; };
var obj7: D | string;
if (obj7 instanceof D) { // narrowed to D.
obj7.foo;
obj7.bar;
}
var obj8: any;
if (obj8 instanceof D) { // can't narrow type from 'any'
obj8.foo;
obj8.bar;
}
// a construct signature that returns a union type
interface EConstructor {
new (): E1 | E2;
}
interface E1 {
foo: string;
bar1: number;
}
interface E2 {
foo: string;
bar2: number;
}
declare var E: EConstructor;
var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1.
obj9.foo;
obj9.bar1;
obj9.bar2;
}
var obj10: any;
if (obj10 instanceof E) { // can't narrow type from 'any'
obj10.foo;
obj10.bar1;
obj10.bar2;
}
// a construct signature that returns any
interface FConstructor {
new (): any;
}
interface F {
foo: string;
bar: number;
}
declare var F: FConstructor;
var obj11: F | string;
if (obj11 instanceof F) { // can't type narrowing, construct signature returns any.
obj11.foo;
obj11.bar;
}
var obj12: any;
if (obj12 instanceof F) { // can't narrow type from 'any'
obj12.foo;
obj12.bar;
}
// a type with a prototype, it overrides the construct signature
interface GConstructor {
prototype: G1; // high priority
new (): G2; // low priority
}
interface G1 {
foo1: number;
}
interface G2 {
foo2: boolean;
}
declare var G: GConstructor;
var obj13: G1 | G2;
if (obj13 instanceof G) { // narrowed to G1. G1 is return type of prototype property.
obj13.foo1;
obj13.foo2;
}
var obj14: any;
if (obj14 instanceof G) { // can't narrow type from 'any'
obj14.foo1;
obj14.foo2;
}
// a type with a prototype that has any type
interface HConstructor {
prototype: any; // high priority, but any type is ignored. interface has implicit `prototype: any`.
new (): H; // low priority
}
interface H {
foo: number;
}
declare var H: HConstructor;
var obj15: H | string;
if (obj15 instanceof H) { // narrowed to H.
obj15.foo;
obj15.bar;
}
var obj16: any;
if (obj16 instanceof H) { // can't narrow type from 'any'
obj16.foo1;
obj16.foo2;
}