Derik Whittaker

Syndication

News


Putting the Dynamic back into TypeScript

One of the really powerful features that TypeScript brings to the JavaScript world is some Static Typing, even if it is only at code/compile time.  With TypeScript I can say that an object is of a certain type which implies there are only a limited set of properties/methods available to me.  This is great, but it does limit the core power of JavaScript and dynamic languages in general.

Given the static nature of TypeScript, (yes I can choose to ignore this feature, but why?) which I want in most cases, the following will cause me compile time issues.

image

The reason this is causing me issues is because MemberInitContext does NOT contain a LoadChoachingStaffStats property, but I know that the caller may provide an object which does contain this object.   Below is my definition for MemberInitContext.

image

Given I know this property MAY be provided at runtime I have a decision to make.  I can do any of the following to resolve my build errors.

  • Add the property to the MemberInitContext
  • Create a inherited type which contains this property
  • Change my constructor to take an ‘any’ rather than a type
  • Add the dynamic back into TypeScript

I am going to choose the last option here, but how?  Simple, by redefining the variable to an any INSIDE my constructor as below.

image

By declaring my type as an ‘any’ TypeScript is happy and lets me hack away and complete my tasks.

Long story shot is that even though TypeScript tries to impose Static typing, which is AWESOME in most cases, you can still force it back to dynamic if you want.

Till Next Time,

P.S. Check out my HTML for the XAML developer course on Pluralsight


Posted 05-08-2013 5:56 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Abc wrote re: Putting the Dynamic back into TypeScript
on 05-08-2013 7:18 AM

IF caller may call it with some optional property THEN

change class to interface with optional member...

interface Friend {

   name: string;

   favoriteColor?: string;

}

Derik Whittaker wrote re: Putting the Dynamic back into TypeScript
on 05-08-2013 8:26 AM

The issue w/ that is then future developers are left to wonder WHY is this optional string on the object.

But your right, that is one of many options.

AbraaoAlves wrote re: Putting the Dynamic back into TypeScript
on 05-08-2013 9:14 AM

Or caller the member like dictionary:

if(initContext['LoadCoachingStaffStats']){

 ...

}

Sorry, but I do not believe that typescript impose static typing. After all you don't impose something giving options.

S Thomas wrote re: Putting the Dynamic back into TypeScript
on 05-09-2013 6:27 AM

Yeah, all you've done is a cast-to-any to subvert the type checking in TypeScript. It's a hack and the question you have to ask is : am I doing this because I have to or because I'm lazy?

Derik Whittaker wrote re: Putting the Dynamic back into TypeScript
on 05-09-2013 6:31 AM

Thomas,

You could not be more right, i am doing it to subvert the type checking.  As for lazy... hell yea, all developers are lazy. :)

More importantly I am doing it because i can and can blog it.  Others will want to do the same thing for what ever reason they have.

Oh, and it just works ;)

William Clardy wrote re: Putting the Dynamic back into TypeScript
on 05-09-2013 9:54 AM

Being a former copy editor as well as a devout believer in static typing, I can't help but infer a relationship between your dynamic typing and your dynamic spelling.

"Till" is something a farmer or gardener does in preparing the land for seeding -- and etymologically unrelated to "Until" or its informal contracted form: "'Til". ;-)

Derik Whittaker wrote re: Putting the Dynamic back into TypeScript
on 05-09-2013 10:01 AM

@William,

Ha, I would advise you not to copy edit my stuff as you may be consumed and lose all ability to have free time :)

Till Next Time,

Rob wrote re: Putting the Dynamic back into TypeScript
on 05-09-2013 5:09 PM

If you wanted to do the cast inline, the syntax is:

   if((<any>initContext).LoadCoachingStaffStats){

... and I think that what you've blogged is a perfectly valid approach. Typescript is a gradually typed language, which means you can use types when they help but it's up to you when you use them.

Cam wrote re: Putting the Dynamic back into TypeScript
on 05-10-2013 6:38 PM

I find I use the approach that @Rob mentioned.  If I'm using a static object there is a reason for it (from a code quality perspective) and I want to isolate the places where I specifically leave that envelope down to each individual instance (makes it more clear for when I come back a year later).

Also every min engine out there will automatically remove my extra parenthesis but wouldn't remove your extra variable.  So effectively doing it in line is potentially better at runtime and definitely requires less javascript code on the client (which is an important goal for fast sites).

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)