I have new empty ASP MVC 5.0 project created in Visual Studio. I have added following nuget packages:
my tsconfig.json file:
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"wwwroot"
]
}
When my project is set to use TypeScript 2.2 and I build the project then everything works but if I change to use any newer version for example 2.7 I am receiving around 34 errors in knockout.d.ts file like this:
Property 'push' of type '(...items: T[]) => void' is not assignable to string index type 'KnockoutBindingHandler'
Property 'remove' of type '{ (item: T): T[]; (removeFunction: (item: T) => boolean): T[]; }' is not assignable to string index type 'KnockoutBindingHandler'
To workaround this problem I have downloaded new knockout definition from https://www.npmjs.com/package/@types/knockout and replace the file added by nuget and now I have no errors.
My questions: 1. Are the typescript definitions nuget packages up to date? 2. how to configure Visual studio project to use npm
Are the typescript definitions nuget packages up to date?
No - The knockout.d.ts provided in the nuget package will not compile using newer versions of the TypeScript compiler, probably breaking around 2.4 or 2.5, so it is out of date. This answer has some info on that https://stackoverflow.com/a/45569371/678338
how to configure Visual studio project to use npm
I would uninstall the knockout.TypeScript.DefinitelyTyped nuget package and make sure that the ~\Scripts\typings\knockout\knockout.d.ts file is gone. Assuming you have node/npm installed on your machine do the following in the root of your webapp
npm init
npm install --save @types/knockout
then a .ts file like this will compile without errors (and you should get intellisense on the ko reference)
import * as ko from 'knockout'
class Test {
name = ko.observable();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With