amcharts5 icon indicating copy to clipboard operation
amcharts5 copied to clipboard

Typings in amCharts 5 for example - dataContext: unknow

Open tscislo opened this issue 1 year ago • 3 comments

In general I observed that amCharts 5 is poorly typed. In many cases props are typed as unknown which causes issues with TypeScript based projects. I think that the most prominent example is dataContext being typed as unknown in many cases.

tscislo avatar Aug 10 '22 14:08 tscislo

We use unkown in a a very few places. In those places where we cannot enforce any kind of structure, like dataContext that you mentioned, which is a raw user data. We cannot possibly know what kind of mumbo-jumbo users could have in their data, and we can't possibly restrict that.

Hope that makes sense.

martynasma avatar Aug 10 '22 15:08 martynasma

You could use generic types for this. With current version of AmCharts in our TS code we need to use @ts-ignore a lot

tscislo avatar Aug 12 '22 12:08 tscislo

@tscislo Originally we did try using generics, but it caused a lot of extra complexity and problems, because the generic has to be used all over the place.

In most cases TypeScript can't infer the correct type anyways, and the data is often retrieved with JSON (which has no static types), so it wasn't worth it.

If you know the static type of the data, you can easily use as to convert it:

interface IFoo {
    foo: string;
    bar: number;
}

const foo = data.dataContext as IFoo;

Except for user data (which can be anything), amCharts is very good at static types.

You should never need to use @ts-ignore, if you are then there is something very wrong. None of our examples use @ts-ignore.

Pauan avatar Aug 13 '22 09:08 Pauan

This issue is stale because it has been open 30 days with no activity. It will be closed in 5 days unless a new comment is added.

github-actions[bot] avatar Sep 13 '22 00:09 github-actions[bot]

have it accept a type argument <T> or just use a generic type.

It's completely weird to being throwing ts-ignore all over that place when using amcharts

daniel-ricado avatar Apr 16 '24 22:04 daniel-ricado