all symbols within a 'with' block will be typed as 'any'

Topics: Language Specification
Nov 17, 2012 at 5:45 AM
Edited Nov 17, 2012 at 6:06 AM

The with statement is only partially supported:

 

module a{
	export function b():void{
		alert("yo");
	}
}

with (a){
	b();	
}

 

It works, but as the compiler says "all symbols within a 'with' block will be typed as 'any'".

Can we expect this to change? It would be really nice to be able to have the types also within 'with's.

(Edit: I just realized that 'with' is not allowed in Ecmascript 5 strict mode.)

Coordinator
Nov 20, 2012 at 3:15 PM

This is unlikely to change. 

As you pointed out, it's fallen out of favor (it's in the Bad Parts in Crockford's book http://oreilly.com/javascript/excerpts/javascript-good-parts/bad-parts.html). 

Nov 20, 2012 at 4:21 PM
Edited Nov 20, 2012 at 4:22 PM

So you are saying that we can't use one of the features in Javascript when using TypeScript?

That is pretty much crap because I have variables that need to be casted and the structure of the modules will make it pretty long. And I have to set multiple properties inside those variables. Which and without the with statement it forces me to repeat it 10 times in a row.

Coordinator
Nov 21, 2012 at 3:04 PM

One way to think of TypeScript is a linting tool for JavaScript (plus a few features).  You can always ignore the suggestions TypeScript has - in a sense type errors are really only type warnings.  For example, you might want to do:

var x = {} + {}

It's perfectly valid JavaScript.  TypeScript will give you a warning, but it will still output the JavaScript you requested.

Conversely, TypeScript doesn't give you typing help in a few cases and instead goes to 'any'.  This helps to simplify typechecking so that the compiler can use type inference more often, and to help that inference be pretty efficient.  It's a trade-off.