Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
valjean Avatar asked Oct 20 '25 12:10

valjean


1 Answers

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.

like image 198
Titian Cernicova-Dragomir Avatar answered Oct 22 '25 03:10

Titian Cernicova-Dragomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!