sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

Working with VSCode Intellisense #153

Closed jensenhertz closed 2 years ago

jensenhertz commented 2 years ago

I have a typescript project where I'm using ObjectModel. I love ObjectModel and it's what I always wanted Typescript types to be, but I'm having hard time using it to its fullest. I would love to use it for all interfaces, but because IntelliSense can't parse it, I'm now stuck doing a sort of hybrid approach where I do shared models with both ObjectModel and TS interfaces, and all functions use only TS interfaces. After having IntelliSense showing function arguments, etc. for years, it's a bit hard to step back to not seeing anything.

Are there any examples how to either get ObjectModel to work with IntelliSense or how to some way integrate ObjectModel with TS?

sylvainpolletvillard commented 2 years ago

That's actually a key difference between static and dynamic type checking. Static type checking is done at build time, and implies that all types are known in advance. Therefore your IDE can provide type-checking and autocomplete, because all types are static. That's called static code analysis.

While dynamic type-checking is done at runtime, and types can actually change dynamically. To give you a simple example, here is a Model that acts as a Number or a String depending on a coin flip:

const quanticModel = Model([Number, String]).assert(x => typeof x === (Math.random() < 0.5 ? "string" : "number"))

So it's impossible to provide Intellisense or other features related to static code analysis with dynamic type-checking solutions like ObjectModel. And it's impossible to provide type-checking based on values defined at runtime with solutions like TypeScript. Static and dynamic type-checking solutions are used for different purposes and objectives. In my current projects, I usually end up using both of them (TypeScript for core, internal logic, and ObjectModel for external interfaces controller)

jensenhertz commented 2 years ago

Ah, makes sense. I didn't think about the fact you can do dynamic types. I've been also going to the direction using OM to validate external inputs because doing both TS and OM types is time consuming. Thanks for the explanation!