cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Small question about Tutorial App

henk_stobbe
DynaMight Champion
DynaMight Champion

Hello,

(do no worries everything works now (-;)

Small question about below,

    const formatter: FormatOptions<Unit, ConvertableUnit> = {
    maximumFractionDigits: chartPrecision,

This means we have a formatter of type FormatOptions expecting two props.

Where in the documentation can I find the FormatOptions type?

I assume this is the interface input-ToUnid and output-FromUnit but I guess this is defined in the Runtime,

Am I missing something?

KR Henk

5 REPLIES 5

stefan_eggersto
Dynatrace Mentor
Dynatrace Mentor

Hi again 🙂

You can find the type reference at https://developer.dynatrace.com/reference/sdks/units/#formatoptions 

The properties of FormatOptions tell the formatter how it should behave, for example, to which unit it should convert the input or if it should abbreviate the number.

Stefan please make my day😀

Looking at your link,

henk_stobbe_0-1696938121166.png

Is const formatter: FormatOptions<ToUnit, ConvertableUnit>

Not a better, or am I still missing something?

 

KR Henk

stefan_eggersto
Dynatrace Mentor
Dynatrace Mentor

Hi @henk_stobbe ,

It took me some time to dig into that since the typing of this package is all but trivial. Long story short:

  • The documentation is mixing types (e.g. ConvertableUnit) and type parameters (FromUnit and ToUnit). Only types should be documented there.
  • In the tutorial, the definition of formatter needs to refer to types, so only const formatter: FormatOptions<Unit, ConvertableUnit> is valid, but not const formatter: FormatOptions<FromUnit, ToUnit>

 

Now the longer story with some background information 🙂 The relevant parts of the type definition of FormatOptions is following:

export interface FormatOptions<FromUnit extends Unit, ToUnit extends ConvertableUnit> {
...
    input?: FromUnit;
    output?: ToUnit;
...
}
This means:
  • FormatOptions is a generic interface with two type parameters
  • Unit and ConvertableUnit are types provided by the SDK. units.currency.usd satisfies the type Unit, so you can write the following valid assignment: const myUnit: Unit = units.currency.usd;
  • FromUnit and ToUnit are just the names of the type parameters and only valid within this type definition
  • According to this signature, FromUnit needs to be any type that extends Unit, ToUnit needs to be any type that extends ConvertableUnit
To sum it up: Unit and ConvertableUnit are relevant for consumers, ToUnit and FromUnit are just the names of the type parameters of FormatterOptions.
 
You can read more on generics in TypeScript at https://www.typescriptlang.org/docs/handbook/2/generics.html

Hello Stefan,

I realy wish I could give you more the one Kudo, great answer. Sorry for giving you the extra work.

It helped met to better understand, 

Thx! 

Great answer (tip) - thank you!

Have a nice day!

Featured Posts