Welcome! Please see the About page for a little more info on how this works.

+2 votes
in Clojure by

I am a big fan of Clojure for its simplicity, muti-thread models, language stability, and excellent Java interop. But I am also an even bigger fan of static typing, which is extremely helpful for projects with more than a few developers.

TypeScript adds an excellent layer of type check for JS that could help eliminate many programming errors at compile time. And it also opens up lots of editor / ide tooling possibilities. I would say that TypeScript is the best thing ever happened to JS.

I am wondering if Clojure core team or any party with serious capability and resources is considering developing something like TypeScript for Clojure?

If not, why?

Thanks

1 Answer

+5 votes
by

The Clojure core team has no plans to work on static typing for Clojure as we don't think it's necessary. Our effort is focused in spec (https://clojure.org/about/spec), which provides support for describing optional specifications for Clojure data and functions.

The Typed Clojure project (https://typedclojure.org/) is a fairly large effort by Ambrose Bonnaire-Sergeant that was done while he was working on his PhD etc. You may also be interested in Spectrum (https://github.com/arohner/spectrum) which is static type checking based on specs. Neither of these are widely used or in particularly active development afaik.

by
Very helpful. Thanks
by
edited by
I think there is a misunderstanding here:
"static typing, which is extremely helpful for projects with more than a few developers"
"The Clojure core team has no plans to work on static typing for Clojure as we don't think it's necessary. Our effort is focused in spec (https://clojure.org/about/spec)"

AFAIU, and please correct me if I'm wrong, poster is asking for "Design time types", while the answer deals with static types as understood by Java.

What I, and, possibly, question author, are looking for is to write
 
(defn describe-dog [^:happy-puppy-co.api/dog dog]
      (println (str (:breed dog) (:age dog))))

and have intellisense pickup  :happy-puppy-co.api/dog spec and provide hints based on its keys and their respective subspecs. I.E. help me use ::dog as its author intended without having to go look into its file.

TypeScript does not exist post compilation, its output is JavaScript. The biggest benefit of TS is that it helps you understand other peoples code a lot faster (and provides some help with avoiding misuse too, but that is secondary).
by
It's not at all clear to me that your comment aligns with the original question, but if you are asking for specs integrated with function definitions, that is something we're looking at in spec 2.
by
Well, how else can the following be interpreted?

"I am ... fan of static typing, which is extremely helpful for projects with more than a few developers.

TypeScript ... also opens up lots of editor / ide tooling possibilities. I would say that TypeScript is the best thing ever happened to JS."

 AFAIU, this means constantly having to work with a lot of code that neither you, nor even your team wrote or reviewed previously. It's typical for enterprise apps to have dozens of dtos with 30+ keys and very inconsistent naming. Typical thought-process goes like "Ok, our function will get a 'sales-deal' dto, I need to convert it to 'financing-deal' and sent to analytics, how was our profit margin property called on 'sales-deal'? Was it 'margin' or 'markup' or just plain 'rate'? And was it a decimal already or did sales-deals still have it as itemized rate components sub-dto?" And then the same set of questions for 'financing-deal'...

I totally second the "best thing that happened to JS part". Sadly, trying ClojureScript feels like going back to JS - not good. I appreciate the power of spec, and how it helps me at runtime and testing. What I would like to do now is take all that power and plug it into my IDE to also help me with writing code. There is nothing to run or test if I haven't written it yet.
by
P.S. To better illustrate, what I'm looking for, check this library: https://github.com/vriad/zod
It lets you define spec-like schemas that are also understood and usable by TS compiler during design/compile time.

// spec definition - exists after TS compilation and can be used to verify schema
const dogSchema = z.object({
  name: z.string(),
  neutered: z.boolean(),
});

// verify schema in runtime
const cujo = dogSchema.parse({
  name: 'Cujo',
  neutered: true,
}); // passes, returns Dog

//TypeScript type definition - does not exist after TS compilation
type Dog = z.infer<typeof dogSchema>;
/*
equivalent to:
type Dog = {
  name:string;
  neutered: boolean;
}
*/

// use inferred type for compile-time type-checking and design-time intellisense
const fido: Dog = {
  name: 'Fido',
}; // TypeError: missing required property `neutered`
...