2

We are using typescript for one of our larger project and we ran into a bug due to the fact that typescript allows any to be passed when expecting an interface.

Why does typescript allow this or is there a setting that i missed.

The following code sample compiles correctly

interface IInterface{
    InterfaceProperty:string;
}

var prop:any = "2000";

function DoStuff(a:IInterface)
{
    var x = a.InterfaceProperty;
}

// Why am i allowed to pass any as an interface?
DoStuff(prop);

1 Answer 1

4

From the Typescript handbook:

Any

We may need to describe the type of variables that we do not know when we are writing an application. These values may come from dynamic content, e.g. from the user or a 3rd party library. In these cases, we want to opt-out of type-checking and let the values pass through compile-time checks. To do so, we label these with the any type:

In short with any, you opt-out of type-checking

Sign up to request clarification or add additional context in comments.

1 Comment

Of course... we will need to turn on --noImplicitAny and make sure people don't use any. In my mental image any was like Object in C#, however its more like dynamic in C#. Thanks. Simmilar to question stackoverflow.com/questions/32227277/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.