Rusty Divine

Live, Love, Learn, Teach

How to Enforce Explicit TypeScript Types in Visual Studio 2013

TypeScript has a compiler flag –noImplicitAny to make sure you have explicitly typed all of your variables, even if you type is as “any”.

//With --noImplicitAny flag set, this is OK
class Student {
    fullname: string;
    constructor(public firstname : any, public middleinitial : string, 
        public lastname : string) {
        this.fullname = firstname + " " + middleinitial + " " + lastname;
    }
}

//With --noImplicitAny flag set, this will throw a compile error:

class Student {
    fullname: string;
    constructor(public firstname, public middleinitial, public lastname) {
        this.fullname = firstname + " " + middleinitial + " " + lastname;
    }
}

In order to set this flag in Visual Studio 2013, edit your project’s properties and then in the TypeScript Build menu, uncheck the Allow implicit ‘any’ types.

TS_implicit_any

With the check box unchecked, your compiler will catch any instance where you did not declare the variable type in your TypeScript.

blog comments powered by Disqus