Why does accessing a property of indexOf still compile?
I made a typo in TypeScript which was picked up during code review.
I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject).
I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.
Can anyone explain this?
typescript methods properties
add a comment |
I made a typo in TypeScript which was picked up during code review.
I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject).
I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.
Can anyone explain this?
typescript methods properties
1
What did you assign the result of? Because what you wrote is valid you are taking thesomeObjectmember of theindexOfmethod. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.
– vlaz
1 hour ago
1
Welcome to javascript, where everything is an object!
– Jean-Baptiste Yunès
1 hour ago
2
@DeWetvanAs I am actually curious about your problem - this seems like a genuine bug/problem with TypeScript see example here. It seems that if you are trying to assign to a variable of typenumber, the result of.indexOf[someObject]shouldn't be considered anumberand thus the compilation would fail. That's the whole idea of TypeScript is - to enforce the types. The answers here focus on JS but ignore this.
– vlaz
43 mins ago
1
@DeWetvanAs You might want to not accept an answer yet if none of them explains why you are not getting a TypeScript error.
– Bergi
29 mins ago
@Bergi As the question has underlined more and more the TS side, I've edited my answer! I think this should explain it :)
– sjahan
2 mins ago
add a comment |
I made a typo in TypeScript which was picked up during code review.
I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject).
I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.
Can anyone explain this?
typescript methods properties
I made a typo in TypeScript which was picked up during code review.
I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject).
I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.
Can anyone explain this?
typescript methods properties
typescript methods properties
edited 33 mins ago
Bergi
366k58546872
366k58546872
asked 1 hour ago
De Wet van AsDe Wet van As
817
817
1
What did you assign the result of? Because what you wrote is valid you are taking thesomeObjectmember of theindexOfmethod. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.
– vlaz
1 hour ago
1
Welcome to javascript, where everything is an object!
– Jean-Baptiste Yunès
1 hour ago
2
@DeWetvanAs I am actually curious about your problem - this seems like a genuine bug/problem with TypeScript see example here. It seems that if you are trying to assign to a variable of typenumber, the result of.indexOf[someObject]shouldn't be considered anumberand thus the compilation would fail. That's the whole idea of TypeScript is - to enforce the types. The answers here focus on JS but ignore this.
– vlaz
43 mins ago
1
@DeWetvanAs You might want to not accept an answer yet if none of them explains why you are not getting a TypeScript error.
– Bergi
29 mins ago
@Bergi As the question has underlined more and more the TS side, I've edited my answer! I think this should explain it :)
– sjahan
2 mins ago
add a comment |
1
What did you assign the result of? Because what you wrote is valid you are taking thesomeObjectmember of theindexOfmethod. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.
– vlaz
1 hour ago
1
Welcome to javascript, where everything is an object!
– Jean-Baptiste Yunès
1 hour ago
2
@DeWetvanAs I am actually curious about your problem - this seems like a genuine bug/problem with TypeScript see example here. It seems that if you are trying to assign to a variable of typenumber, the result of.indexOf[someObject]shouldn't be considered anumberand thus the compilation would fail. That's the whole idea of TypeScript is - to enforce the types. The answers here focus on JS but ignore this.
– vlaz
43 mins ago
1
@DeWetvanAs You might want to not accept an answer yet if none of them explains why you are not getting a TypeScript error.
– Bergi
29 mins ago
@Bergi As the question has underlined more and more the TS side, I've edited my answer! I think this should explain it :)
– sjahan
2 mins ago
1
1
What did you assign the result of? Because what you wrote is valid you are taking the
someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.– vlaz
1 hour ago
What did you assign the result of? Because what you wrote is valid you are taking the
someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.– vlaz
1 hour ago
1
1
Welcome to javascript, where everything is an object!
– Jean-Baptiste Yunès
1 hour ago
Welcome to javascript, where everything is an object!
– Jean-Baptiste Yunès
1 hour ago
2
2
@DeWetvanAs I am actually curious about your problem - this seems like a genuine bug/problem with TypeScript see example here. It seems that if you are trying to assign to a variable of type
number, the result of .indexOf[someObject] shouldn't be considered a number and thus the compilation would fail. That's the whole idea of TypeScript is - to enforce the types. The answers here focus on JS but ignore this.– vlaz
43 mins ago
@DeWetvanAs I am actually curious about your problem - this seems like a genuine bug/problem with TypeScript see example here. It seems that if you are trying to assign to a variable of type
number, the result of .indexOf[someObject] shouldn't be considered a number and thus the compilation would fail. That's the whole idea of TypeScript is - to enforce the types. The answers here focus on JS but ignore this.– vlaz
43 mins ago
1
1
@DeWetvanAs You might want to not accept an answer yet if none of them explains why you are not getting a TypeScript error.
– Bergi
29 mins ago
@DeWetvanAs You might want to not accept an answer yet if none of them explains why you are not getting a TypeScript error.
– Bergi
29 mins ago
@Bergi As the question has underlined more and more the TS side, I've edited my answer! I think this should explain it :)
– sjahan
2 mins ago
@Bergi As the question has underlined more and more the TS side, I've edited my answer! I think this should explain it :)
– sjahan
2 mins ago
add a comment |
3 Answers
3
active
oldest
votes
Quite easy.
someArray.indexOf you know that this is a function, which is also an object and can have properties.
By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.
Of course, it is not defined on the indexOf function, so it returns undefined.
Quick example that illustrates the syntax and the fact that a function can have properties ;) :
const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);EDIT
Here is an attempt of an answer for the TypeScript side of the question.
As you already know, TypeScript is designed to be compatible with JavaScript. Therefore, as in JS, you can access a property of an object by the following ways:
- 'Statically':
obj.property
- 'Dynamically':
obj['property']
Both ways will perform the same, but with the dynamic way of accessing the property, there is no way TypeScript compiler can determine the type of it or if it exists or not. That's why it doesn't raise an error.
(Of course, someone could say the compiler could understand that here, the compiler could read the string 'property', but don't forget that you could pass any variable and its value could change at run time, far outside of the TypeScript scope.)
By using the 'static' way to access a property, of course, TypeScript will raise an error!
Hoping this helps ;)
3
I think answers here are missing theTypeScripttag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you haveindex: numberandindex = arr.indexOf[obj]then that should be a compilation error. Butindex: anywouldn't throw a compilation error.
– vlaz
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of theundefinedresult but the main question remains...
– Florian
1 hour ago
add a comment |
array.indexOf is a function.
Functions are objects.
You were accessing the someObject property of the array.indexOf function.
You would have got undefined.
const array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefined
1
Array.indexOfisundefined,Array.prototype.indexOf, on the other hand, is a function.
– Pavlo
39 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
add a comment |
Functions in JavaScript are first class objects.
When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.
1
What about TypeScript?
– vlaz
1 hour ago
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54254396%2fwhy-does-accessing-a-property-of-indexof-still-compile%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Quite easy.
someArray.indexOf you know that this is a function, which is also an object and can have properties.
By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.
Of course, it is not defined on the indexOf function, so it returns undefined.
Quick example that illustrates the syntax and the fact that a function can have properties ;) :
const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);EDIT
Here is an attempt of an answer for the TypeScript side of the question.
As you already know, TypeScript is designed to be compatible with JavaScript. Therefore, as in JS, you can access a property of an object by the following ways:
- 'Statically':
obj.property
- 'Dynamically':
obj['property']
Both ways will perform the same, but with the dynamic way of accessing the property, there is no way TypeScript compiler can determine the type of it or if it exists or not. That's why it doesn't raise an error.
(Of course, someone could say the compiler could understand that here, the compiler could read the string 'property', but don't forget that you could pass any variable and its value could change at run time, far outside of the TypeScript scope.)
By using the 'static' way to access a property, of course, TypeScript will raise an error!
Hoping this helps ;)
3
I think answers here are missing theTypeScripttag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you haveindex: numberandindex = arr.indexOf[obj]then that should be a compilation error. Butindex: anywouldn't throw a compilation error.
– vlaz
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of theundefinedresult but the main question remains...
– Florian
1 hour ago
add a comment |
Quite easy.
someArray.indexOf you know that this is a function, which is also an object and can have properties.
By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.
Of course, it is not defined on the indexOf function, so it returns undefined.
Quick example that illustrates the syntax and the fact that a function can have properties ;) :
const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);EDIT
Here is an attempt of an answer for the TypeScript side of the question.
As you already know, TypeScript is designed to be compatible with JavaScript. Therefore, as in JS, you can access a property of an object by the following ways:
- 'Statically':
obj.property
- 'Dynamically':
obj['property']
Both ways will perform the same, but with the dynamic way of accessing the property, there is no way TypeScript compiler can determine the type of it or if it exists or not. That's why it doesn't raise an error.
(Of course, someone could say the compiler could understand that here, the compiler could read the string 'property', but don't forget that you could pass any variable and its value could change at run time, far outside of the TypeScript scope.)
By using the 'static' way to access a property, of course, TypeScript will raise an error!
Hoping this helps ;)
3
I think answers here are missing theTypeScripttag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you haveindex: numberandindex = arr.indexOf[obj]then that should be a compilation error. Butindex: anywouldn't throw a compilation error.
– vlaz
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of theundefinedresult but the main question remains...
– Florian
1 hour ago
add a comment |
Quite easy.
someArray.indexOf you know that this is a function, which is also an object and can have properties.
By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.
Of course, it is not defined on the indexOf function, so it returns undefined.
Quick example that illustrates the syntax and the fact that a function can have properties ;) :
const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);EDIT
Here is an attempt of an answer for the TypeScript side of the question.
As you already know, TypeScript is designed to be compatible with JavaScript. Therefore, as in JS, you can access a property of an object by the following ways:
- 'Statically':
obj.property
- 'Dynamically':
obj['property']
Both ways will perform the same, but with the dynamic way of accessing the property, there is no way TypeScript compiler can determine the type of it or if it exists or not. That's why it doesn't raise an error.
(Of course, someone could say the compiler could understand that here, the compiler could read the string 'property', but don't forget that you could pass any variable and its value could change at run time, far outside of the TypeScript scope.)
By using the 'static' way to access a property, of course, TypeScript will raise an error!
Hoping this helps ;)
Quite easy.
someArray.indexOf you know that this is a function, which is also an object and can have properties.
By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.
Of course, it is not defined on the indexOf function, so it returns undefined.
Quick example that illustrates the syntax and the fact that a function can have properties ;) :
const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);EDIT
Here is an attempt of an answer for the TypeScript side of the question.
As you already know, TypeScript is designed to be compatible with JavaScript. Therefore, as in JS, you can access a property of an object by the following ways:
- 'Statically':
obj.property
- 'Dynamically':
obj['property']
Both ways will perform the same, but with the dynamic way of accessing the property, there is no way TypeScript compiler can determine the type of it or if it exists or not. That's why it doesn't raise an error.
(Of course, someone could say the compiler could understand that here, the compiler could read the string 'property', but don't forget that you could pass any variable and its value could change at run time, far outside of the TypeScript scope.)
By using the 'static' way to access a property, of course, TypeScript will raise an error!
Hoping this helps ;)
const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);edited 3 mins ago
answered 1 hour ago
sjahansjahan
3,2051827
3,2051827
3
I think answers here are missing theTypeScripttag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you haveindex: numberandindex = arr.indexOf[obj]then that should be a compilation error. Butindex: anywouldn't throw a compilation error.
– vlaz
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of theundefinedresult but the main question remains...
– Florian
1 hour ago
add a comment |
3
I think answers here are missing theTypeScripttag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you haveindex: numberandindex = arr.indexOf[obj]then that should be a compilation error. Butindex: anywouldn't throw a compilation error.
– vlaz
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of theundefinedresult but the main question remains...
– Florian
1 hour ago
3
3
I think answers here are missing the
TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.– vlaz
1 hour ago
I think answers here are missing the
TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.– vlaz
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of the
undefined result but the main question remains...– Florian
1 hour ago
@vlaz +1. sjahan gives OP a quick explanation of the
undefined result but the main question remains...– Florian
1 hour ago
add a comment |
array.indexOf is a function.
Functions are objects.
You were accessing the someObject property of the array.indexOf function.
You would have got undefined.
const array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefined
1
Array.indexOfisundefined,Array.prototype.indexOf, on the other hand, is a function.
– Pavlo
39 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
add a comment |
array.indexOf is a function.
Functions are objects.
You were accessing the someObject property of the array.indexOf function.
You would have got undefined.
const array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefined
1
Array.indexOfisundefined,Array.prototype.indexOf, on the other hand, is a function.
– Pavlo
39 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
add a comment |
array.indexOf is a function.
Functions are objects.
You were accessing the someObject property of the array.indexOf function.
You would have got undefined.
const array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefinedarray.indexOf is a function.
Functions are objects.
You were accessing the someObject property of the array.indexOf function.
You would have got undefined.
const array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefinedconst array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefinedconst array = [1, 2, 3]
const someObject = 'asdasd'
console.log(array.indexOf[someObject])
// undefinededited 33 mins ago
answered 1 hour ago
0xc14m1z0xc14m1z
1,409512
1,409512
1
Array.indexOfisundefined,Array.prototype.indexOf, on the other hand, is a function.
– Pavlo
39 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
add a comment |
1
Array.indexOfisundefined,Array.prototype.indexOf, on the other hand, is a function.
– Pavlo
39 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
1
1
Array.indexOf is undefined, Array.prototype.indexOf, on the other hand, is a function.– Pavlo
39 mins ago
Array.indexOf is undefined, Array.prototype.indexOf, on the other hand, is a function.– Pavlo
39 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
You’re right! I fixed the typo in my answer. Thanks
– 0xc14m1z
32 mins ago
add a comment |
Functions in JavaScript are first class objects.
When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.
1
What about TypeScript?
– vlaz
1 hour ago
add a comment |
Functions in JavaScript are first class objects.
When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.
1
What about TypeScript?
– vlaz
1 hour ago
add a comment |
Functions in JavaScript are first class objects.
When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.
Functions in JavaScript are first class objects.
When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.
answered 1 hour ago
GibboKGibboK
34.3k107317542
34.3k107317542
1
What about TypeScript?
– vlaz
1 hour ago
add a comment |
1
What about TypeScript?
– vlaz
1 hour ago
1
1
What about TypeScript?
– vlaz
1 hour ago
What about TypeScript?
– vlaz
1 hour ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f54254396%2fwhy-does-accessing-a-property-of-indexof-still-compile%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
1
What did you assign the result of? Because what you wrote is valid you are taking the
someObjectmember of theindexOfmethod. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.– vlaz
1 hour ago
1
Welcome to javascript, where everything is an object!
– Jean-Baptiste Yunès
1 hour ago
2
@DeWetvanAs I am actually curious about your problem - this seems like a genuine bug/problem with TypeScript see example here. It seems that if you are trying to assign to a variable of type
number, the result of.indexOf[someObject]shouldn't be considered anumberand thus the compilation would fail. That's the whole idea of TypeScript is - to enforce the types. The answers here focus on JS but ignore this.– vlaz
43 mins ago
1
@DeWetvanAs You might want to not accept an answer yet if none of them explains why you are not getting a TypeScript error.
– Bergi
29 mins ago
@Bergi As the question has underlined more and more the TS side, I've edited my answer! I think this should explain it :)
– sjahan
2 mins ago