Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout Typescript definition errors for TS > 2.2

I have new empty ASP MVC 5.0 project created in Visual Studio. I have added following nuget packages:

  • knockout.TypeScript.DefinitelyTyped 1.1.6
  • knockoutjs 3.4.2

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

like image 648
Konrad Avatar asked Feb 24 '18 13:02

Konrad


1 Answers

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();
}
like image 159
Dr Blowhard Avatar answered Sep 29 '22 08:09

Dr Blowhard