How to avoid name clash with basic type keywords in TypeScript
svdHero opened this issue · 2 comments
Hi there,
I have only started with TypeScript and front-end development recently and I discovered the decoders
package just now.
Quick question: What is the recommend way to import the basic decoders such as object
, number
, string
, etc.? All these are built-in TypeScript keywords. Should I just rename the imports or is there any better technique?
Maybe this question could also become a feature request for export the basic decoders with some wrapper object called JsonDecoders
which can then be imported and used like JsonDecoders.object
, JsonDecoders.number
, JsonDecoders.string
, etc.
Just for convenience when using IDEs with auto-import, e.g. VSCode.
You should be able to just use them by importing them like so:
import { object, string, Decoder } from 'decoders';
type Thing = {
name: string
};
const thingDecoder: Decoder<Thing> = object({
name: string,
});
The string
on L4 is used in a type position, whereas the string
on L8 is used in a value position, so they won’t ever conflict! 🙂
If you configured your editor with TypeScript support, autocomplete should already work for these imports.
If you prefer to import them on a single object, you can use:
import * as d from 'decoders';
const thingDecoder: Decoder<Thing> = d.object({
name: d.string,
});
But I prefer the former.
Thanks, I actually like the import * as d from 'decoders'
to be more explicite. I'll go with
import * as decoders from "decoders"
to help me distinguish between type number
and decoder decoder.number
, etc. mentally. My nooby brain needs this. 😆