Search posts, tags, users, and pages
Hi Manh Do, I gave a little example in BaseModel constructor, to create readonly attributes using Object.defineProperty for BirdModel. You could also use Reflect, or even Proxy depends on your case.
You could also extends or modify the code to add more function helpers like what ActiveStorage have in Rails.
The idea here is to leverage typescript declaration merging to make it possible with typescript.
Maybe the title isn’t specific enough, I’ve changed it already. Thanks for your feedback.
CHI PUTERA
Ah, I see. Thanks for clarifying.
I'm also interested in this topic. Do you think we can do something like macro in typescript?
Say I need to gather all file names in a given folder and make it be a union, something like:
type AllFileName = FilesAsUnion<`./path/to/folder`>
// emit: AllFileName = 'foo' | 'bar' | 'baz' | ...
of course, I can write a script to watch the folder and emit generated type. But it looks not that good
Manh Do Unfortunately, we can't define or inject types dynamically on the fly.
I don't think there is another way but to create a types-generator for that.