Repository navigation
Description
The current TypeScript introspector is quite limited and remove a bunch of interesting feature that are natively available and broadly uses by TS engineers such as the complete type system, generics and complex default value.
The global idea is to make the TS SDK DX much more natural to a TS engineer.
For example, this simple snippet isn't working with dagger but should after this improvement:
type User = {
name: string
age: number
}
const DEFAULT_NAME = "john"
@object()
export class Test {
// This is broken because `User` will not be registered in the Dagger API.
@func()
greet(user: User): string {
return `Hey I'm ${user.name}, ${user.age.toString()} years old`
}
// This is broken because `DEFAULT_NAME` isn't resolved during the introspection.
@func()
newPerson(name = DEFAULT_NAME): User {
return { name, age: 19 }
}
}Right now, you need to create a class User, exposing field using decorator and creating a constructor if you don't want to assign fields one by one. It's possible to hack it using Object.assign but it's doesn't feels right and it's not commonly used in TypeScript project.
Then why don't we just supports the typekeyword?
The answer is simple: how do we know if the type should actually be exposed to the API or not? We managed that with decorators like @object() and @func() but type, enum and interface keywords doesn't support decorators.
So if we aim to support these keywords, we would need to resolve types & value when they are references.
How does this work
If we reuse the same example, the flow of introspection would be:
- Get the module name, convert it to PascalCase with will result in
Test - Search for the
Testclass in the user's module code (verify that it's decorated) - Register existing functions and detect if there's a reference to an external declaration
- Lookup all reference (here it would be
UserandDEFAULT_NAME) and register them (for default value it's just resolving what the value is) - Continue recursively until there's no more reference.
What does it enable
This would also enable constructing complex nested type without a huge boilerplate such as
type File = {
name: string
size: number
}
type Directory = {
files: File[]
path: string
}
Instead of
@object()
class File {
@func()
name: string
@func()
size: number
constructor(
name: string
size: number
) {
this.name = name
this.size = size
}
}
// ... I think it's pretty explicit for the restAnd also indeed, resolve default value in a more accurate way.
It would also be an occasion to improve the introspection error handling to give more hint to the user.
For example: type XXX is referenced in function XXX but isn't exposed to the dagger API. Please decorate your class with @object(), same for specific types that are not supported yet like type Status = "online" | "offline".
Specs of the new supported TS features
type
Can be used to create dagger object but we can imagine supporting union types also (in a simple way for now)
// A dagger object with a field name & age
// For now properties are public (but there might be ways to actually make it private with generics)
type User {
name: string
age: number
}enum
Resolving references would also enabling using native TypeScript eum, instead of our current noisy notation.
It's not perfect though since you cannot add metadata yet, but this can be solved with generic (gonna write an issue soon about that)
// This could be a better notation for enum also (can be supported in a follow up)
enum Status {
Online = "online"
Offline = "Offline"
}💡 interface keyword will not be supported yet since it's pretty confusing with actual Dagger interface. TS interface allows to create data structure like type but also containing function, it's more or less a class with only public properties and no constructor. Making it very hard to pick in the context of Dagger.
Referenced default value
Thanks to #8653, the introspection will be done in 2 phases.
- Check what's exported from the user's module (any function, class etc... can be resolved, meaning that an exported default value can be resolved and registered to the GraphQL!)
- Resolve class, values and use the introspector for specific data.
### Tasks
- [ ] Start introspection by importing user's module
- [ ] List references of the main class
- [ ] Convert `type` to dagQL notation (object...)
- [ ] Convert `enum` to dagQL enum
- [ ] Correctly resolve complex default value (and document the limits)
- [ ] Rework tests to map the new way
- [ ] Ensure backward compatibility <- Should be okay
- [ ] Document all new features supported
Activity
Xiot commentedon Oct 11, 2024
I am very excited for this. Wanted to point out some incorrect assumptions with this though.
The differences between
typeandinterfaceare actually a lot more subtle that you described. Both of them can have fields and methods, and both can be composed (extends / implements)The
interfacein Typescript is pretty much like an interface in 'normal' OOP languages. It simply defines the shape of an object, but doesn't supply any of the implementation.interfaces can also be extended, so they can have "partial" definitions in separate files.typecan be as simple as you have in your original post, or they can get really complex. The type system is actually turing complete.A very simple usage of the type system would be
Partialis built into the language, but its definition looks something likeThat iterates through all of the properties of
Tand makes them optional. So,is the same as
I'm not suggesting having full support for these types, but just wanted to expand your knowledge on them a little bit more
TomChv commentedon Oct 14, 2024
I totally agree! However in the context of dagger, an
Interfaceis the same as aGointerface, which is quite different that OOP since you cannot have any property in anGo interface. Originally, I wanted to support bothtypeandinterfacebut after talking with some of my peers and seeing how much they got lost betweeninterface&dagger interface, I rollbacked my idea.Of course! I tried to kept it scope of what we want to support. It would be too much of work for now to support specific
Partial,Require,Pick,Omitetc... but maybe one day ;)Xiot commentedon Oct 14, 2024
This is where some of my concerns come in on the ts-sdk side, since most thinks, it seems (from my perspective, anyway) is based on go, and not on typescript. It's the same as the
@field()vs@func().I would assume that the majority of people using the typescript sdk, would have a lot more experience with typescript than go, so having some go'isms creep into the ts-sdk feels a little out of place.
TomChv commentedon Oct 15, 2024
So you think I should support both
interfaceandtypebut then how would you define a dagger interface?If
interfacecan be use to describe a dagger object, what's left to define a dagger interface?Xiot commentedon Oct 15, 2024
I don't really know enough about a "dagger interface" to be of much help for that question, but I would think that
interfaceanddag.Interfacewould be different, unrelated things.I'm just saying that some people would instinctively use
interfacewhen definining their local types. You could just support one, but it does still get a little messy. Especially ifinterfaceis overloaded to be restricted todag.InterfaceTomChv commentedon Oct 17, 2024
UPDATE
I'm dropping some tests I did to check if importing module could help on the introspection and it seems were very limited with the current configuration:
This is the example I've used:
Code
[ [Module: null prototype] { TestEnum: { A: 'a', B: 'b' }, defaultEnum: 'a' }, [Module: null prototype] { References: [class References] } ]However, this doesn't include types and interfaces.
Based on some research, it's a normal behaviour and we shouldn't disable strict mode to access these data.
In conclusion
I can use the import logic to actually resolve exported value, which can be useful for default value resolution but I'm pretty much stuck on the introspection side. I'll need to do it with the basic TypeScript compiler API.
I'll continue following this path, if by any chance there's a better way, please let me know, I'm writing code that should be easily replacable for that purpose.
Xiot commentedon Oct 17, 2024
If you are just using javascript, then you definitely won't access to the types.
For this stuff it will all need to be with the compiler api.
There is also a way to get the compiler api to simplify a type for you. So it would take something like
and give you
I can't remember the exact way off of the top of my head, but it did involve getting the symbols for the types IIRC
TomChv commentedon Oct 17, 2024
This will definitely be helpful! I almost at this part haha
Xiot commentedon Oct 17, 2024
That would also simplify the 'do we use
typeorinterfacefordag.Interface' question.As long as you can simplify the type, it doesn't matter which one is used.
There is also ts-morph which may help out in using the compiler api
see: https://stackoverflow.com/a/68623960
This may help as well. The fact that its a cli doesn't help much, but it does basically achieve what you need
https://github.com/FormidableLabs/ts-simplify
TomChv commentedon Oct 17, 2024
I'm currently hitting an issue with
ts-morph, I'll need to change the way I actually resolve typedef in case of intersection.I opened an issue, hopefully I can get unblock fast on that because it's another level of difficulity to do an hybrid resolution: dsherret/ts-morph#1580
TomChv commentedon Oct 21, 2024
Update related to the Type resolution:
I'm having an issue with
ts-morph, he's not able to properly resolve some types for some reason so I'll need to change my strategie and fallback to backward resolution.Basically the idea is:
Because sometime,
ts-morphwill think a type is something while it's actually another, example:Here's in the first case, type is correctly resolve but not in the other because the default value seems to impact the type resolution (ts-morph tries to do some inference but it doesn't really work well).
TomChv commentedon Oct 24, 2024
Ts-morph is doing such a weird job on type resolution.. it can resolve a node to a complete different type and it's not consistent...
That's so strange...
I've complete the reference propagation, it works perfectly! Now I'm fighting with ts-morph not giving me the correct type for an argument.
Same for return type:
And event for properties
feat: typescript SDK resolve value by reference.
feat: typescript SDK resolve value by reference.
feat: typescript SDK resolve value by reference.
feat: typescript SDK resolve value by reference.
feat: typescript SDK resolve value by reference.
feat: typescript SDK resolve value by reference.
feat: typescript SDK resolve value by reference.