valjean
valjean

Reputation: 53

How to prevent implicit conversion from 'any' on function call in Typescript

Consider the following typescript code:

function eatString(str: string){
    console.log(str);
}

const anyObject: any = {
    junk: 3425234,
};

eatString(anyObject); // Compiles ok - by why?
eatString({something: "abc"}); // Doesn't compile - as expected

Is there a way to prevent the function eatString(str: string) from taking an any argument, either by tsconfig or tslint options or otherwise?

I initially thought that noImplicitAny might help but after trying it and reviewing the documentation it wasn't what I thought. no-any isn't an option for me as I still want to be able to use any in some cases.

If this isn't possible, is there some reason that I'm missing as to why? I haven't been working in typescript/javascript for very long, but I've already been caught out a few times by some issues that this would have prevented.

Upvotes: 4

Views: 1819

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249506

any by definition is assignable to any other type, so when you pass anyObject to the parameter str it will be compatible as per this rule.

You should avoid using any unless absolutely necessary. If you don't know the type you should use unknown which is not compatible to other types without a guard or an assertion (see here for differences with any)

function eatString(str: string){
    console.log(str);
}

const anyObject: unknown = {
    junk: 3425234,
};

eatString(anyObject); // error now

In this particular case you should just let the compiler infer the type for anyObject

function eatString(str: string){
    console.log(str);
}

const anyObject = { // inferred as { junk : number }
    junk: 3425234,
};

eatString(anyObject); // error now

You could use tslint to forbit the usable of any as a type annotation (using this rule) but any might stillleak in from external APIs.

Upvotes: 4

Related Questions