It appears straightforward since the same code functions well in a simple JS file and provides autocompletion for the ko variable's members. Here is the TypeScript code snippet:
// both of the following import lines result in: `ko` undefined
// import { ko } from "@tko/build.knockout";
import { ko } from "../node_modules/@tko/build.knockout/dist/build.knockout.es6";
alert("test: " + ko);
tsconfig.json
{
"compilerOptions": {
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "js",
"target": "ES3",
"watch": true,
"allowJs": true,
"lib": ["ES5", "DOM"],
"module": "CommonJS"
},
"include": [
"ts"
],
"exclude": [
"node_modules"
]
}
The repository for testing can be found here.
While using the provided tsconfig.json file, I encountered difficulties importing the ko4 package effectively. Adjustments to the tsconfig.json were necessary, but ensuring compatibility with all utilized modules in the main project posed a challenge. Opting for ES6 import syntax was my choice due to its future-proof nature.
I originally intended to use KnockOut v3.5, however it does not support ES6 import syntax.
Furthermore, please note that I utilize VS Code for development.
Thank you.
Update 1
(referring to Nenad's response)
To resolve issues, I had to adjust moduleResolution to "Node" within tsconfig.json (previously set to the default, "classic" in my case).
Additionally, creating a package.json
file in the project's root directory was essential. Despite believing that I already had one, it turned out I only had a package-lock.json
. After including the package.json
, running npm i
again allowed VS Code to interpret the imports without necessitating specific paths inside the node_modules
directory; simply referencing the npm module name sufficed.
Moreover, replacing instances of KnockoutObservable
with ko.Observable
, along with other similarly named classes and interfaces used in the project, was imperative.
An outstanding issue pertains to the AMD module system switch resulting in inadequate inclusion of required modules within the output file (bundle.js); currently an unresolved matter requiring further exploration.