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.
typescript tslint
add a comment |
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.
typescript tslint
The whole idea ofanyis 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
add a comment |
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.
typescript tslint
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
typescript tslint
asked 22 hours ago
valjean
8123
8123
The whole idea ofanyis 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
add a comment |
The whole idea ofanyis 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
add a comment |
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.
Somehow I missed theunknowntype, which in combination with tslint'sno-anysolves my problem. Initially I was seeinganyas whatunknownreally 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
add a comment |
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.
Somehow I missed theunknowntype, which in combination with tslint'sno-anysolves my problem. Initially I was seeinganyas whatunknownreally 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
add a comment |
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.
Somehow I missed theunknowntype, which in combination with tslint'sno-anysolves my problem. Initially I was seeinganyas whatunknownreally 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
add a comment |
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.
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.
edited 22 hours ago
answered 22 hours ago
Titian Cernicova-Dragomir
51.8k33148
51.8k33148
Somehow I missed theunknowntype, which in combination with tslint'sno-anysolves my problem. Initially I was seeinganyas whatunknownreally 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
add a comment |
Somehow I missed theunknowntype, which in combination with tslint'sno-anysolves my problem. Initially I was seeinganyas whatunknownreally 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
The whole idea of
anyis 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