2

I am attempting to update my knockout typings, as latest version of typescript is is detecting problems with the older ones.

The problem comes with trying to get the knockout and knockout.es5 typings working together.

I am using VS2017 and a tsconfig file.

I install knockout typings like so:

typings install dt~knockout

and then I install the knockout-es5 typings like so:

typings install dt~knockout-es5 --global

After I install the knockout es5 typings, they have compile errors because there is no such type KnockoutObservable<T>.

So I added this to the top of the knockout-es5 typings:

/// <reference path="../../modules/knockout/index.d.ts" />
import * as ko from "knockout";

and changed KnockoutObservable<any> to ko.Observable<any> and it now compiles.

So.. now I want use these typings to create a knockout view model, so in a new typescript file:

import * as ko from "knockout"

class Foo {
    Id: number
    Name: string
    constructor(id: number, name: string) {
        this.Id = id;
        this.Name = name;
        ko.track(this);
    }
}

This results in compile error as track does not exist on typeof ko

This is because ko is imported from the knockout module, which is exported from the knockout typings, however track is a method that is meant to be added by the knockout-es5 typings, but isn't. How do I amend the knockout-es5 typings file further to get this to work?

Update: I have tried adding this to the bottom of the knockout.es5/index.d.ts file:

declare var ko: KnockoutStatic;

declare module "knockout-es5" {
    export = ko;
}

And in my typescript file I am trying:

import * as ko from "knockout-es5"

But the import statement won't compile: It says cannot find module knockout-es5.

I have also tried this:

import * as ko from "../typings/globals/knockout.es5/index"

This seems to work, but then I have to change my code to do this, which seems wrong to me (Note the additional .ko:

class Application {
    Id: number
    Name: string
    constructor(id: number, name: string) {
        this.Id = id;
        this.Name = name;

        ko.ko.track(this);
    }
}

And also once I do that, I can no longer resolve the original ko types like ko.Observable.

Darrell
  • 1,905
  • 23
  • 31

1 Answers1

0

I think what your looking for is.

import ko from 'knockout'

ko is the default object and will import alone.

When you use this way, you are importing all the objects, and not just ko.

import * as ko from "knockout-es5"

If you are going to use that way you are probably looking for it this way to pull ko out of the full object.

import { ko } from "knockout-es5"
Mardok
  • 1,360
  • 10
  • 14