4

I've got the following in Javascript:

var chartOptions = {
    chartType: settings.chartType,
}

chartOptions.func = function(chart) {
    chartOptions.fullChart = chart;
}

which all works fine.

But I want to change this to TypeScript and being new, I am unsure about how to do this. Please can someone help? For instance, there is no symbol for 'func' and none for 'fullChart' so how would this have worked in Javascript?

2
  • 1
    Its unclear what you're asking here. Are you asking for an explanation of the code? What does typescript have to do with this? What do you mean there is no symbol for 'func' or 'fullChart'? You've declared them as properties of the chartOptions object. Commented Dec 15, 2015 at 12:50
  • When I put the above logic into Typescript, the compiler comes back with an error that it cannot resolve the symbols 'func' and 'fullChart'. What do I need to do to correct this? Commented Dec 15, 2015 at 12:58

2 Answers 2

5

You can fix your code adding the properties to chartOptions:

var chartOptions = {
    chartType: settings.chartType,
    func: null,
    fullChart: null
}

chartOptions.func = function(chart) {
    chartOptions.fullChart = chart;
}

What about converting your code to a class? It's much more TypeScript-y :)

class ChartOptions {
    public chartType;
    public fullChart;

    public func(chart: any) {
        this.fullChart = chart 
    }
}    

let chart = {};
let chartOptions = new ChartOptions();
chartOptions.func(chart);
Sign up to request clarification or add additional context in comments.

2 Comments

What about "constructor(public chartType: any) { }"? Or any certain type instead of any for the ctor argument?
@TSV: I don't use these advanced features such as ...(public chartType...) when I try to answer a question to a beginner. It only causes more confusion IMO. Without knowing more about OP's code it's just guessing..
0

Consider using an interface to define your options before you set them up. To mirror your specific code snippet, you can use a type assertion here.

interface ChartOptions {
    chartType: string;
    func: (chart: any) => void;
    fullChart: any;
}

var chartOptions = {
    chartType: settings.chartType
} as ChartOptions

chartOptions.func = function(chart) {
    chartOptions.fullChart = chart;
}

It really depends on the end goal, but if you can set up the options to begin with you won't need the type assertion (avoid it where possible anyway).

interface ChartOptions {
    chartType: string;
    func: (chart: any) => void;
    fullChart?: any;
}

var chartOptions: ChartOptions = {
    chartType: settings.chartType,
    func (chart: any) {
        this.fullChart = chart;
    }
}

Or you can use a class as the other user suggested, which is more like what you should be using here, but not what your example is using.

Comments

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.