You can import a json document directly into typescript and the entire document is a readonly constant type of the entire json object literal.
I've used this with a template JSON document that has an instance of all possible types in the runtime json. Sometimes you might have to loosen the type slightly, but it works great for a quick start.
However, there are Java frameworks that basically generate a type system from your database so whatever columns you query ends up being static types that can be determined at compile time. https://www.jooq.org/ Java is really quite cool. I wonder if we'll ever get a typescript version of jOOQ
I think we eventually will be able to evaluate JS in context of types and return whatever we want even asynchronously. Now we have some combinators given by TS authors which allow us to compose some limited set of computations. I don't know how Java does it, but I hope TS will do it better :-)
if we are talking about code gen then it exists in every language, nothing extraordinary here. In nodejs, there is prisma for example. This typescript thing generates static type on the fly, not by generating code.
Interestingly this is something you can do in Zig with its `@Type`. Zig's compiler can eval (some) Zig code at compile time, this code can produce new types, and you can mention those types anywhere types are normally mentioned.
Is this just storing data in memory and querying it with SQL?
I use DynamoDB Data Mapper in production (https://github.com/awslabs/dynamodb-data-mapper-js) - would be cool to see something like this for SQL. Effectively define a class with TS decorators to automatically generate tables and columns.
I'm interested to see if I can use these new template string type features to add more/better type checking and inference to my TS Postgres library[1]. My suspicion is that the TS recursion depth limit of 50 might limit this rather seriously.
Comments (191) 32 threads
Earlier commentsJust curios, are there real-world use cases for stuff like this?
It's a very niche use-case, this is definitely more of an impressive showcase of the new Template Literal Types feature of TS :)
I've used this with a template JSON document that has an instance of all possible types in the runtime json.
Sometimes you might have to loosen the type slightly, but it works great for a quick start.
However, there are Java frameworks that basically generate a type system from your database so whatever columns you query ends up being static types that can be determined at compile time. https://www.jooq.org/
Java is really quite cool. I wonder if we'll ever get a typescript version of jOOQ
[1]: https://diesel.rs/ [2]: https://crates.io/crates/sqlx
I use DynamoDB Data Mapper in production (https://github.com/awslabs/dynamodb-data-mapper-js) - would be cool to see something like this for SQL. Effectively define a class with TS decorators to automatically generate tables and columns.
> MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js
[1] https://jawj.github.io/zapatos/
All comments shown.
Couldn't load more comments. Check your connection and try again.