It seems like the issue lies with the 'options' problem here:
var dropdown = $('#cars');
dropdown.options = function(data, selectedKey) ...
This code works in JavaScript, but when using TypeScript, you might encounter an error due to static type-checking. Since dropdown
is a jQuery object and jQuery type definitions do not declare an options
property, assigning to it will result in an error. To work around this, you can typecast dropdown
to any
so that it behaves like a regular JavaScript object:
var dropdown: any = $('#cars');
dropdown.options = (...)
The same error occurs with .Default
where you are initializing storedPreferences
as:
var storedPreferences = localStorage.getItem(...);
Since TypeScript infers the return type of localStorage.getItem
as string
, it treats storedPreferences
as a string. In TypeScript, you cannot change the type of a variable once declared, hence the error when trying to access a non-existent property like Default
.
To solve this, you can explicitly declare storedPreferences
as type any
:
let storedPreferences: any = localStorage.getItem(...);
The main issue here is that you're coding in TypeScript as if it were JavaScript, leading to compiler errors. Remember that in TypeScript, you cannot add new properties to objects of known types without declaring them as any
first. This way, TypeScript will behave more like JavaScript, but you lose some advantages of TypeScript.
Additionally, it's recommended to use let
or const
instead of var
.
The code on jsfiddle functions correctly because it is written in JavaScript, not TypeScript.