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











up vote
0
down vote

favorite












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.










share|improve this question






















  • The whole idea of any is that it it is the one type that can represent any JavaScript value with no constraints. github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.1
    – Oram
    22 hours ago

















up vote
0
down vote

favorite












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.










share|improve this question






















  • The whole idea of any is that it it is the one type that can represent any JavaScript value with no constraints. github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.1
    – Oram
    22 hours ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question













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.







typescript tslint






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 22 hours ago









valjean

8123




8123












  • The whole idea of any is that it it is the one type that can represent any JavaScript value with no constraints. github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.1
    – Oram
    22 hours ago




















  • The whole idea of any is that it it is the one type that can represent any JavaScript value with no constraints. github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.1
    – Oram
    22 hours ago


















The whole idea of any is that it it is the one type that can represent any JavaScript value with no constraints. github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.1
– Oram
22 hours ago






The whole idea of any is that it it is the one type that can represent any JavaScript value with no constraints. github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.1
– Oram
22 hours ago














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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 partivular 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.






share|improve this answer























  • Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
    – valjean
    20 hours ago










  • @valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
    – Titian Cernicova-Dragomir
    20 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371041%2fhow-to-prevent-implicit-conversion-from-any-on-function-call-in-typescript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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 partivular 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.






share|improve this answer























  • Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
    – valjean
    20 hours ago










  • @valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
    – Titian Cernicova-Dragomir
    20 hours ago















up vote
0
down vote



accepted










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 partivular 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.






share|improve this answer























  • Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
    – valjean
    20 hours ago










  • @valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
    – Titian Cernicova-Dragomir
    20 hours ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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 partivular 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.






share|improve this answer














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 partivular 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 22 hours ago

























answered 22 hours ago









Titian Cernicova-Dragomir

51.8k33148




51.8k33148












  • Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
    – valjean
    20 hours ago










  • @valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
    – Titian Cernicova-Dragomir
    20 hours ago


















  • Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
    – valjean
    20 hours ago










  • @valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
    – Titian Cernicova-Dragomir
    20 hours ago
















Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
– valjean
20 hours ago




Somehow I missed the unknown type, which in combination with tslint's no-any solves my problem. Initially I was seeing any as what unknown really is, but I understand now. Thanks!
– valjean
20 hours ago












@valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
– Titian Cernicova-Dragomir
20 hours ago




@valjean glad to help, don't forget to upvote and mark as answered if it was useful :)
– Titian Cernicova-Dragomir
20 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371041%2fhow-to-prevent-implicit-conversion-from-any-on-function-call-in-typescript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Saint-Aignan (Tarn-et-Garonne)

Volksrepublik China

How to test boost logger output in unit testing?