Element implicitly has an 'any' type because type 'MyFancyType' has no index signature.",
class MyPerson {
public name: string;
public subscriptions: MyFancyType[];
constructor(name: string, subscriptions: MyFancyType){...}
}
// usage
someFunction(index: number, isCondition: boolean, value: any){
const dynPropertyName = isCondition? 'price' : 'expiration'
const person = new Person(...);
person.subscriptions[index][dynPropertyName] = value;
^^^^^^^^^^^^^^^^^ Error: implicit any ... no index signature
}
person.subscriptions[index][dynPropertyName as keyof MyFancyType] = value;
Because TypeScript does not know that MyFancyType DOES have the properties 'price' and 'expiration'.
Alternative Solution:
interface IMySomething {
someKey: number;
anotherKey: Date;
[key: string]: number; // allow basically any string property with an value type of value
}
Tell TypeScript that an object of the type 'IMySomething' can have any string property and the assigned value of the string property is a number.
Property 'value' does not exist on type EventTarget in TypeScript
<input id="myInput" value="I typed something...">
<script>
document.getElementById('myInput').addEventListener('change', function(event){
const newValue = event.target.value;
^^^^^^ Error: Property does not exist on type EventTarget
})
</script>
const newValue = (event.target as HTMLInputElement).value
// or
const newValue = (<HTMLInputElement>event.target).value
Because TypeScript does not know, that the target HtmlElement is of type input. Not every HtmlElement has a value property.
Event Object is possibly 'null'.
and Property 'selectedIndex' does not exist on type 'EventTarget'.
SomeComponent.vue inside of ...
<select @input="someChange($event)">
<option>some option</option>
</select>
SomeComponent.vue inside of <script lang="ts">...</script>
export default Vue.extend({
//...
methods: {
someChange(event: InputEvent) {
const index = event.target.selectedIndex;
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Object is possibly 'null'.
^^^^^^^^^^^^^^ Property 'selectedIndex' does not exist on type 'EventTarget'.
}
}
}
onChange(event: InputEvent) {
const index = (event.target as HTMLSelectElement).selectedIndex;
}
Type 'HTMLCollectionOf<...>' is not an array type or a string type / Property 'array method' Does not exist on type HTMLCollection<...>
Type 'HTMLCollectionOf<HTMLSomeElement>' is not an array type or a string type
Property 'push/pop/forEach/filter/map' Does not exist on type HTMLCollection<...>
<select multiple id="mySelect" onchange="myFunction(event)">
<option value="Bread">Bread
<option value="Butter">Butter
<option value="Jam">Jam
</select>
const selectedCollection = (document.getElementById("mySelect") as HTMLSelectElement).selectedOptions;
const selectedArray = [...selectedCollection]
^^^^^^^^^^^^^^^^^^ Type 'HTMLCollectionOf<...>' is not an array type or a string type
const lastElement = selectedCollection.pop();
^^^ Property 'pop' does not exist on type 'HTMLCollectionOf<...>'.
Use Array.from()
like this:
const selectedCollection = (document.getElementById("mySelect") as HTMLSelectElement).selectedOptions;
const selectedArray = Array.from(selectedCollection);
const lastElement = selectedArray.pop();
Because HTMLCollections are array-like but not real arrays. They can be accessed with brackets myCollection[index]
but TypeScript does not allow standard array methods. To convert the collection to a real array Array.from()
can be used.