amcharts5
amcharts5 copied to clipboard
Typings in amCharts 5 for example - dataContext: unknow
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.
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.
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 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
.
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.
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