3

I found a possible answer to my question: Is any JavaScript code a valid TypeScript code?.

I am working on a web project with Visual Studio and TypeScript plugin. Inside the following post, I have found this answer:

Not any valid JavaScript code is valid TypeScript see my example below.

var testVar = 4; testVar = "asdf";

TypeScript gives the following error: Cannot convert string to number. To make that work in TypeScript add ":any" like below.

var testVar: any = 4; testVar = "asdf"

This happens because TypeScript noticed testVar is declared and in the declaration it is assigned a number and therefore it decides it should stay a number.

I am trying to use HighCharts. If I save the code inside Javascript file, I dont get any error. But when I save the code inside Typescript file, I have an error:

"Cannot find name "HighCharts".

Here you can see the relevant part of my code:

// Put definitions of highcharts, highstocks below this line
// Example 1 Chart
var initializeChart1 = function () {
    require(['jquery', 'highcharts', 'highchartsMore', 'highchartsExporting'], function ($) {

        $("#myChart").highcharts({

            chart: {
                type: 'spline',
                backgroundColor: '#ECECEC',

// ------THIS GIVES AN ERROR, BUT WORKING WHEN SAVED AS .JS file
                animation: Highcharts.svg, // don't animate in old IE
// -------------------------

...

The reason why I want to keep my JavaScript code inside TS file is simple: I am currently learning TypeScript and I keep JavaScript and TypeScript versions or same code in same file. Depending on if TypeScript is complete/working, I uncomment it and comment JavaScript.

My questions are:

  1. Why I get this error?
  2. Is it possible to use any JavaScript code inside TS files?
3
  • Do you add reference to d.ts files ? github.com/DefinitelyTyped/DefinitelyTyped/tree/master/… and github.com/DefinitelyTyped/DefinitelyTyped/blob/master/… Commented Feb 5, 2016 at 11:45
  • 1
    I made it working by adding "Highcharts" reference in require(['jquery', 'highcharts', 'highchartsMore', 'highchartsExporting'], function ($, Highcharts) {. But I am still confused why this code is working without this reference when saved in .js file but not working when saved in .ts file... Commented Feb 5, 2016 at 11:51
  • 1
    the reference is just there to help you to write code in a strong typed way. And it's necessary to typescript to point you about possibles runtime errors. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code. check this answer to have a simply good idea of typescript stackoverflow.com/questions/12694530/… Commented Feb 5, 2016 at 12:00

1 Answer 1

4

Yes you can use js code in your typescript files but the compiler needs to know about the objects and libraries you use. You have two options to make this compile. Either add the .d.ts file for your library or to fool the compiler you can declare the Highcharts object yourself like this.

declare var Highcharts: any;
var initializeChart1 = function () {
    require(['jquery', 'highcharts', 'highchartsMore', 'highchartsExporting'], function ($) {

        $("#myChart").highcharts({

            chart: {
                type: 'spline',
                backgroundColor: '#ECECEC',
                animation: Highcharts.svg, // don't animate in old IE
Sign up to request clarification or add additional context in comments.

1 Comment

still necessary with requirejs to do it like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.