2014-07-13 01:04:16 +02:00
//// [propertyAccess.ts]
2015-01-21 22:55:07 +01:00
class A {
a : number ;
}
class B extends A {
b : number ;
}
2014-07-13 01:04:16 +02:00
enum Compass {
North , South , East , West
}
var numIndex : { [ n : number ] : string } = { 3 : 'three' , 'three' : 'three' } ;
var strIndex : { [ n : string ] : Compass } = { 'N' : Compass . North , 'E' : Compass . East } ;
var bothIndex :
{
2015-01-21 22:55:07 +01:00
[ n : string ] : A ;
[ m : number ] : B ;
2014-07-13 01:04:16 +02:00
} ;
function noIndex ( ) { }
var obj = {
10 : 'ten' ,
x : 'hello' ,
y : 32 ,
z : { n : 'world' , m : 15 , o : ( ) => false } ,
'literal property' : 100
} ;
var anyVar : any = { } ;
2015-01-21 22:55:07 +01:00
var stringOrNumber : string | number ;
var someObject : { name : string } ;
2014-07-13 01:04:16 +02:00
// Assign to a property access
obj . y = 4 ;
// Property access on value of type 'any'
anyVar . x = anyVar . y = obj . x = anyVar . z ;
// Dotted property access of property that exists
var aa = obj . x ;
// Dotted property access of property that exists on value's apparent type
var bb = obj . hasOwnProperty ;
// Dotted property access of property that doesn't exist on value's apparent type
var cc = obj . qqq ; // error
// Bracket notation property access using string literal value on type with property of that literal name
var dd = obj [ 'literal property' ] ;
var dd : number ;
// Bracket notation property access using string literal value on type without property of that literal name
var ee = obj [ 'wa wa wa wa wa' ] ;
var ee : any ;
// Bracket notation property access using numeric string literal value on type with property of that literal name
var ff = obj [ '10' ] ;
var ff : string ;
// Bracket notation property access using numeric string literal value on type without property of that literal name
var gg = obj [ '1' ] ;
var gg : any ;
// Bracket notation property access using numeric value on type with numeric index signature
var hh = numIndex [ 3.0 ] ;
var hh : string ;
// Bracket notation property access using enum value on type with numeric index signature
var ii = numIndex [ Compass . South ] ;
var ii : string ;
// Bracket notation property access using value of type 'any' on type with numeric index signature
var jj = numIndex [ anyVar ] ;
var jj : string ;
// Bracket notation property access using string value on type with numeric index signature
var kk = numIndex [ 'what' ] ;
var kk : any ;
// Bracket notation property access using value of other type on type with numeric index signature and no string index signature
2015-01-21 22:55:07 +01:00
var ll = numIndex [ someObject ] ; // Error
2014-07-13 01:04:16 +02:00
// Bracket notation property access using string value on type with string index signature and no numeric index signature
var mm = strIndex [ 'N' ] ;
var mm : Compass ;
var mm2 = strIndex [ 'zzz' ] ;
var mm2 : Compass ;
// Bracket notation property access using numeric value on type with string index signature and no numeric index signature
var nn = strIndex [ 10 ] ;
var nn : Compass ;
// Bracket notation property access using enum value on type with string index signature and no numeric index signature
var oo = strIndex [ Compass . East ] ;
var oo : Compass ;
// Bracket notation property access using value of type 'any' on type with string index signature and no numeric index signature
var pp = strIndex [ < any > null ] ;
var pp : Compass ;
// Bracket notation property access using numeric value on type with no index signatures
var qq = noIndex [ 123 ] ;
var qq : any ;
// Bracket notation property access using string value on type with no index signatures
var rr = noIndex [ 'zzzz' ] ;
var rr : any ;
// Bracket notation property access using enum value on type with no index signatures
var ss = noIndex [ Compass . South ] ;
var ss : any ;
// Bracket notation property access using value of type 'any' on type with no index signatures
var tt = noIndex [ < any > null ] ;
var tt : any ;
// Bracket notation property access using values of other types on type with no index signatures
2015-01-21 22:55:07 +01:00
var uu = noIndex [ someObject ] ; // Error
2014-07-13 01:04:16 +02:00
// Bracket notation property access using numeric value on type with numeric index signature and string index signature
var vv = noIndex [ 32 ] ;
var vv : any ;
// Bracket notation property access using enum value on type with numeric index signature and string index signature
var ww = bothIndex [ Compass . East ] ;
2015-01-21 22:55:07 +01:00
var ww : B ;
2014-07-13 01:04:16 +02:00
// Bracket notation property access using value of type 'any' on type with numeric index signature and string index signature
var xx = bothIndex [ < any > null ] ;
2015-01-21 22:55:07 +01:00
var xx : B ;
2014-07-13 01:04:16 +02:00
// Bracket notation property access using string value on type with numeric index signature and string index signature
var yy = bothIndex [ 'foo' ] ;
2015-01-21 22:55:07 +01:00
var yy : A ;
2014-07-13 01:04:16 +02:00
// Bracket notation property access using numeric string value on type with numeric index signature and string index signature
var zz = bothIndex [ '1.0' ] ;
2015-01-21 22:55:07 +01:00
var zz : A ;
2014-07-13 01:04:16 +02:00
// Bracket notation property access using value of other type on type with numeric index signature and no string index signature and string index signature
2015-01-21 22:55:07 +01:00
var zzzz = bothIndex [ someObject ] ; // Error
var x1 = numIndex [ stringOrNumber ] ;
var x1 : any ;
var x2 = strIndex [ stringOrNumber ] ;
var x2 : Compass ;
var x3 = bothIndex [ stringOrNumber ] ;
var x3 : A ;
2014-07-13 01:04:16 +02:00
//// [propertyAccess.js]
2015-01-21 22:55:07 +01:00
var _ _extends = this . _ _extends || function ( d , b ) {
for ( var p in b ) if ( b . hasOwnProperty ( p ) ) d [ p ] = b [ p ] ;
function _ _ ( ) { this . constructor = d ; }
_ _ . prototype = b . prototype ;
d . prototype = new _ _ ( ) ;
} ;
var A = ( function ( ) {
function A ( ) {
}
return A ;
} ) ( ) ;
var B = ( function ( _super ) {
_ _extends ( B , _super ) ;
function B ( ) {
_super . apply ( this , arguments ) ;
}
return B ;
} ) ( A ) ;
2014-07-13 01:04:16 +02:00
var Compass ;
( function ( Compass ) {
Compass [ Compass [ "North" ] = 0 ] = "North" ;
Compass [ Compass [ "South" ] = 1 ] = "South" ;
Compass [ Compass [ "East" ] = 2 ] = "East" ;
Compass [ Compass [ "West" ] = 3 ] = "West" ;
} ) ( Compass || ( Compass = { } ) ) ;
var numIndex = { 3 : 'three' , 'three' : 'three' } ;
var strIndex = { 'N' : 0 /* North */ , 'E' : 2 /* East */ } ;
var bothIndex ;
2015-02-07 03:45:09 +01:00
function noIndex ( ) { }
2014-07-13 01:04:16 +02:00
var obj = {
10 : 'ten' ,
x : 'hello' ,
y : 32 ,
z : { n : 'world' , m : 15 , o : function ( ) { return false ; } } ,
'literal property' : 100
} ;
var anyVar = { } ;
2015-01-21 22:55:07 +01:00
var stringOrNumber ;
var someObject ;
2014-08-16 00:49:09 +02:00
// Assign to a property access
2014-07-13 01:04:16 +02:00
obj . y = 4 ;
2014-08-16 00:49:09 +02:00
// Property access on value of type 'any'
2014-07-13 01:04:16 +02:00
anyVar . x = anyVar . y = obj . x = anyVar . z ;
2014-08-12 00:54:12 +02:00
// Dotted property access of property that exists
2014-07-13 01:04:16 +02:00
var aa = obj . x ;
2014-08-12 00:54:12 +02:00
// Dotted property access of property that exists on value's apparent type
2014-07-13 01:04:16 +02:00
var bb = obj . hasOwnProperty ;
2014-08-12 00:54:12 +02:00
// Dotted property access of property that doesn't exist on value's apparent type
2014-08-15 22:18:54 +02:00
var cc = obj . qqq ; // error
2014-08-12 00:54:12 +02:00
// Bracket notation property access using string literal value on type with property of that literal name
2014-07-13 01:04:16 +02:00
var dd = obj [ 'literal property' ] ;
var dd ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using string literal value on type without property of that literal name
2014-07-13 01:04:16 +02:00
var ee = obj [ 'wa wa wa wa wa' ] ;
var ee ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric string literal value on type with property of that literal name
2014-07-13 01:04:16 +02:00
var ff = obj [ '10' ] ;
var ff ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric string literal value on type without property of that literal name
2014-07-13 01:04:16 +02:00
var gg = obj [ '1' ] ;
var gg ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric value on type with numeric index signature
2014-07-13 01:04:16 +02:00
var hh = numIndex [ 3.0 ] ;
var hh ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using enum value on type with numeric index signature
2014-07-13 01:04:16 +02:00
var ii = numIndex [ 1 /* South */ ] ;
var ii ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using value of type 'any' on type with numeric index signature
2014-07-13 01:04:16 +02:00
var jj = numIndex [ anyVar ] ;
var jj ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using string value on type with numeric index signature
2014-07-13 01:04:16 +02:00
var kk = numIndex [ 'what' ] ;
var kk ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using value of other type on type with numeric index signature and no string index signature
2015-01-21 22:55:07 +01:00
var ll = numIndex [ someObject ] ; // Error
2014-08-12 00:54:12 +02:00
// Bracket notation property access using string value on type with string index signature and no numeric index signature
2014-07-13 01:04:16 +02:00
var mm = strIndex [ 'N' ] ;
var mm ;
var mm2 = strIndex [ 'zzz' ] ;
var mm2 ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric value on type with string index signature and no numeric index signature
2014-07-13 01:04:16 +02:00
var nn = strIndex [ 10 ] ;
var nn ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using enum value on type with string index signature and no numeric index signature
2014-07-13 01:04:16 +02:00
var oo = strIndex [ 2 /* East */ ] ;
var oo ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using value of type 'any' on type with string index signature and no numeric index signature
2014-07-13 01:04:16 +02:00
var pp = strIndex [ null ] ;
var pp ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric value on type with no index signatures
2014-07-13 01:04:16 +02:00
var qq = noIndex [ 123 ] ;
var qq ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using string value on type with no index signatures
2014-07-13 01:04:16 +02:00
var rr = noIndex [ 'zzzz' ] ;
var rr ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using enum value on type with no index signatures
2014-07-13 01:04:16 +02:00
var ss = noIndex [ 1 /* South */ ] ;
var ss ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using value of type 'any' on type with no index signatures
2014-07-13 01:04:16 +02:00
var tt = noIndex [ null ] ;
var tt ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using values of other types on type with no index signatures
2015-01-21 22:55:07 +01:00
var uu = noIndex [ someObject ] ; // Error
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric value on type with numeric index signature and string index signature
2014-07-13 01:04:16 +02:00
var vv = noIndex [ 32 ] ;
var vv ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using enum value on type with numeric index signature and string index signature
2014-07-13 01:04:16 +02:00
var ww = bothIndex [ 2 /* East */ ] ;
var ww ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using value of type 'any' on type with numeric index signature and string index signature
2014-07-13 01:04:16 +02:00
var xx = bothIndex [ null ] ;
var xx ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using string value on type with numeric index signature and string index signature
2014-07-13 01:04:16 +02:00
var yy = bothIndex [ 'foo' ] ;
var yy ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using numeric string value on type with numeric index signature and string index signature
2014-07-13 01:04:16 +02:00
var zz = bothIndex [ '1.0' ] ;
var zz ;
2014-08-12 00:54:12 +02:00
// Bracket notation property access using value of other type on type with numeric index signature and no string index signature and string index signature
2015-01-21 22:55:07 +01:00
var zzzz = bothIndex [ someObject ] ; // Error
var x1 = numIndex [ stringOrNumber ] ;
var x1 ;
var x2 = strIndex [ stringOrNumber ] ;
var x2 ;
var x3 = bothIndex [ stringOrNumber ] ;
var x3 ;