IDK if carton is the most appropriate place to implement the generation, but I think it could provide at least some helpers to make things work smoothly.
One part of this could be a SwiftPM build tool, which reads a config that specifies a path to .ts and .d.ts files and generates Swift source files providing type-safe wrappers based on this.