C
C#2y ago
CrosRoad95

❔ Source generator, generating type script definitions at compile time

I would like to make a automatic type script definition from code, i consider to use source generator to do that, is it a good idea? is source generator able to do this?
19 Replies
CrosRoad95
CrosRoad95OP2y ago
a bit more context: i'm using https://github.com/microsoft/ClearScript to run js from c#, i made a list of methods, classes that are availiable to use in js and now i would like to loop through all of them and generate types
Anton
Anton2y ago
You probably want swagger and NSwag if you're doing a rest api
CrosRoad95
CrosRoad95OP2y ago
not rest api just plain text file with typescript definition
Thinker
Thinker2y ago
An SG can output non-cs source files afaik.
Anton
Anton2y ago
And if you're doing grpc, you just need to use their compiler
CrosRoad95
CrosRoad95OP2y ago
^ thats what i want i already use grpc lib for it
Anton
Anton2y ago
well it supports generating clients for different languages
CrosRoad95
CrosRoad95OP2y ago
https://andrewlock.net/creating-a-source-generator-part-1-creating-an-incremental-source-generator/ i wonder if it will okey class Foo{ void Bar(){} } do some reflection magic, generate:
interface Foo {
bar(): void
}
interface Foo {
bar(): void
}
Thinker
Thinker2y ago
Are you talking about SGs or reflection
Anton
Anton2y ago
what's your use case
CrosRoad95
CrosRoad95OP2y ago
generate typescript types from c# classes is it able to read comments?
Thinker
Thinker2y ago
yes
CrosRoad95
CrosRoad95OP2y ago
ok, i will try it then
Anton
Anton2y ago
what for
CrosRoad95
CrosRoad95OP2y ago
did they fix so i no longer need to restart vs to see new generated code?
Thinker
Thinker2y ago
nope Also, you might want to consider using an attribute to denote the types which should be targeted
CrosRoad95
CrosRoad95OP2y ago
i'm using ClearScript tags to do it attributes* not tags how can i tell an IIncrementalGenerator to generate content, for example html files? Generator attribute doesn't like "Html" as first parameter
Thinker
Thinker2y ago
Call AddSource on the context inside RegisterSourceOutput although actually... i don't know if you can output things other than cs files Better ask in #roslyn to be safe
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server