nanostores / i18n

A tiny (≈600 bytes) i18n library for React/Preact/Vue/Svelte
MIT License
229 stars 12 forks source link

number, time and count functions type mismatch #10

Closed Fxlr8 closed 2 years ago

Fxlr8 commented 2 years ago
const { number, time } = useStore(format)

The documentation clearly says that they accept a second argument: https://github.com/nanostores/i18n/blob/99d21a5b709db9a1127b2ac529b3765f9be1dce6/README.md?plain=1#L209-L226

But your typings do not agree with that:

image
Fxlr8 commented 2 years ago

Also the count function type says that it returns a function:

image

But if you try to call it, you get this

Uncaught TypeError: formatBanana is not a function
ai commented 2 years ago

It happens because of the error in TypeScript Intl.NumberFormat.format types.

We do not create our own types, but re-use TS types for Intl module https://github.com/nanostores/i18n/blob/main/formatter/index.d.ts#L6-L7

Seems like we need to migrate to custom types.

ai commented 2 years ago

Can you create a separate issue for count?

ai commented 2 years ago

Fixed 2a9cae3

ai commented 2 years ago

Also the count function type says that it returns a function

count should be used only inside i18n({ … }) call.

TypeScript dynamic types can be tricky. I didn’t found a way to solve type problem of converting types inside i18n to t types. Think about it as trade off for types maintainability.

https://github.com/nanostores/i18n/blob/main/create-i18n/index.d.ts#L36-L44

ai commented 2 years ago

Released in 0.2.1.