Ways to avoid any
in generated TypeScript declarations
#3801
richard-viney
started this conversation in
Ideas & suggestions
Replies: 1 comment
-
It cannot be Perhaps an annotation is an option, though I'm not yet sold on this approach. Overall this is extremely low value so I'm unlikely to dedicate much time to the design and review work here. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently the following Gleam code:
generates this TypeScript declaration:
This occurs frequently in the stdlib, e.g. for
Dict
,Dynamic
,Regex
, andStringBuilder
, as well as in other common libraries like thejson.Json
type.I'd like to find a way to avoid
any
in generated TypeScript declarations so that:any
type.These were both points of friction during my initial foray into using the generated TypeScript declarations in a real-world project that imports several Gleam libraries.
Option 1 - Use the
unknown
typeThe
any
type could be changed tounknown
to better reflect what the compiler knows about the type (i.e. nothing), and to give meaningful additional safety. It's not perfect though, as such types would be interchangeable due to all beingunknown
, e.g. in TypeScript aDict
could be passed where only aRegex
is valid. But it's still a big improvement.Changing
any
tounknown
would also not be backwards compatible with TypeScript code that accesses properties or calls methods on one of theseany
types, or relies on it being implicitly converted to another type. However, if these types are opaque (as I understand them to be), then the underlying TypeScript type is a private implementation detail that users shouldn't depend on, so any such existing TypeScript code was never actually well-formed to begin with.Option 2 - Allow the TypeScript type to be provided via
@external
Alternatively, we could allow Gleam code to optionally provide a TypeScript type by allowing
@external
on atype
declaration:Which generates the following TypeScript declaration:
This option gives authors complete control over how these Gleam types are exposed in TypeScript, and it's no longer a concern of the Gleam compiler.
The two options outlined above aren't mutually exclusive. Each could be adopted individually, or both/neither could be adopted. If there are any other approaches for avoiding
any
in the TypeScript declarations that I haven't considered then please let me know - thanks!Beta Was this translation helpful? Give feedback.
All reactions