Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined d3.scale in Typescript

I'm new with Typescript (2 weeks), and I work on project to wrap the d3.js framework. I'm encountered a problem with the usage of "d3.d.ts", namespace, export module, import.

My problem : When I try to use the d3.scale.linear(), I have the error in my browser console :

    TypeError: d3.scale is undefined

The code :

/// <reference path="../typings/d3.d.ts">;
"use strict";
var linear = d3.scale.linear();
console.log("linear resolv !")

My compiler option (I've no error on the compilation process, but perhaps it's interesting) :

{
    "files": [
        "src/twod3/workspace.ts",
        "src/twod3/component/basic.ts",
        "src/twod3/component/data.ts",
        "src/twod3/component/operation.ts",
        "src/main.ts"
    ],
    "compilerOptions": {
        "noImplicitAny": true,
        "target": "es2015",
        "module":"commonjs",
        "sourceMap":true
    }
}

I try different import strategy without success.

The d3.d.ts

declare namespace d3 {
...
    export module scale {
            ...
            export function linear(): Linear<number, number>;
            ...
     }
}

I don't understand the problem, thank you for your help.

like image 591
Rémy Letient Avatar asked Jul 06 '16 07:07

Rémy Letient


2 Answers

Be careful of the version for d3.js In version 4.2.1 d3.scale.linear(); gives the following error. TypeError: d3.scale.linear() scale is undefined

I fixed this error in version 4.2.1 by using d3.scaleLinear();

like image 77
jcoggins Avatar answered Nov 25 '22 22:11

jcoggins


I had a similar kind of issue. I had to match the D3 version, that supported the in built methods for d3. Either match the version of the D3.js, make sure you update your browser or match the in built methods for your current version.

My supported version for d3.scale.ordinal is '3.5.17', previously I was at the latest version.

like image 34
sg28 Avatar answered Nov 26 '22 00:11

sg28