I am currently exploring typescript and attempting to integrate knockout.mapping into my project, but I'm facing some challenges.
Despite installing the necessary libraries for knockout and knockout.mapping, along with their respective "@types" declarations, I still can't seem to get it to function properly.
Within my laravel project, I am utilizing typescript and leveraging laravel mix to compile the JavaScript files.
Below is a snippet of my code:
///<reference path="../../../../node_modules/@types/jquery/index.d.ts"/>
///<reference path="../../../../node_modules/@types/knockout/index.d.ts"/>
///<reference path="../../../../node_modules/@types/knockout.mapping/index.d.ts"/>
import * as ko from "knockout";
import * as $ from "jquery";
$(function(){
// Simple test to verify if ko.mapping exists on the ko object. Unfortunately, it does not appear to be available.
console.log("Message from jQuery Done", (ko));
});
class MyModel {
_data: any;
constructor(the_data: object)
{
let self = this;
let example_observable = ko.observable(); // This works fine
ko.mapping.fromJS(the_data, self._data); // Throws an error - Cannot read property 'fromJS' of undefined
}
}
let myModel = new MyModel({"x": "y"});
ko.applyBindings(myModel);
My package.json file lists the dependencies for knockout and knockout.mapping.
"@types/jquery": "^3.3.6",
"@types/knockout": "^3.4.58",
"@types/knockout.mapping": "^2.0.33",
"ajv": "^6.5.2",
"knockout": "^3.4.2",
"knockout.mapping": "^2.4.3",
I am struggling to comprehend what I might be doing incorrectly.
Any assistance would be greatly appreciated.
Thank you in advance.