ryardley / pdsl

The expressive declarative toolkit for composing predicates in TypeScript or JavaScript
https://pdsl.site
MIT License
69 stars 2 forks source link

TypeScript type compilation #155

Open ryardley opened 4 years ago

ryardley commented 4 years ago

I think the next thing holding back this lib is the lack of automatic typescript support.

One way to manage this would be to change the way TypeScript support is provided and to create a compiler/watcher for auto compiling types to node_modules.

This would allow PDSL to be strongly typed by simply adding a unique typescript key identifier.

const isUser = p<"isUser">`{
  name: string[>2],
  age: > 21
}`

Behind the scenes types would be compiled to somewhere in node modules:

// node_modules/pdsl/___types.ts
export type PDSLTypes = {
  isUser: { name: string, age: number }
}

The type signature of the returned function would look something like this:

type PredicateFn = <T>(input:any) => input is PDSLTypes[T]

Not sure if it is possible to maintain backward compatibility need to look up if conditionals are possible in TypeScript - I have a feeling they might be....

ryardley commented 4 years ago

This would make sense to follow https://github.com/ryardley/pdsl/issues/105