TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The perfect configuration format? Try TypeScript

136 pointsby tmcnealover 3 years ago

44 comments

ljmover 3 years ago
I&#x27;ve only worked with cdk8s, but the experience of that alone was enough for me to regret it. It&#x27;s not that cdk8s is bad, per-se, it&#x27;s that it turned an otherwise declarative kubernetes config into something that was much harder to grok in the first instance.<p>One of the arguments for TS in the post is that it can encourage DRY. It was DRY that made this TS-based kubernetes config so hard to follow, because simple repetitive statements got abstracted into parameterised functions, or classes to inherit from, or other things that are resolved dynamically at runtime. Not to mention that you&#x27;re also going to start mixing in random dependencies from NPM to help abstract things further. This is great for application development but not so great for pure configuration.<p>A language designed for config, like Dhall or CUE, can give you much of what a typed language offers without letting you go crazy with a full-blown language runtime.
评论 #29257398 未加载
评论 #29256730 未加载
评论 #29262950 未加载
SavantIdiotover 3 years ago
Except ....<p>(...No one asked, but here are my $0.02 ;-)<p>You need typescript in your environment to use typescript!<p>The four formats the OP listed can be read by anything from Ada to EXCEL. But typescript can only be ready by a TS build flow.<p>In fact, I think JSON was a bridge too far. A config file shouldn&#x27;t need a <i>compiler</i>.[1]<p>FOR GENERAL PURPOSE: big no.<p>BUT: If this is purely for TS projects where the tooling already exists, then OK. I&#x27;d use it.<p>EDIT [1] I&#x27;m referring to the article&#x27;s early suggestion to run JSON through JSMin to support comments. So transpiling is more apt, not compiling.
评论 #29257595 未加载
评论 #29257115 未加载
评论 #29257221 未加载
realrockerover 3 years ago
Great little writeup ! After mangling YAML, HCL, JSON for years as an ops engineer, I have come to the same realisation. In fact, I have put this into practice in production pipelines by using: jkcfg[1] for the last couple of years. Two data points: 1. Zero developer support contract rate around YAML syntax and templating issues 2. High number of contributions in our private typescript configuration library from developers. Using typescript as an ops frontend has made operations a lot more approachable to folks.<p>Recently I took what learnt in the last 2 years using jkcfg&#x2F;typescript and taken it to Deno in form of an opinionated port of jkcfg called: dxcfg[2]. Its early days, but I would bet on Deno&#x2F;typescript for future ops configuration.<p>[1] <a href="https:&#x2F;&#x2F;jkcfg.github.io&#x2F;#&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jkcfg.github.io&#x2F;#&#x2F;</a> [2] dxcfg: <a href="https:&#x2F;&#x2F;github.com&#x2F;dxcfg&#x2F;dxcfg" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dxcfg&#x2F;dxcfg</a>
escherizeover 3 years ago
Good to see people coming to the same conclusions we have had for a while using Clojure. There&#x27;s an up and coming runtime data validation framework called Malli that I use for these sorts of files.<p>You can get many of the benefits but also run custom validations (dictate how two keys in a map relate to eachother, or force a be of a certain shape instead of just a string).<p>Here&#x27;s the example from the script: [0]<p>[0] - <a href="https:&#x2F;&#x2F;malli.io&#x2F;?value=%7B%20%3Alocale%20%22en-US%22%0A%20%20%3Atimezone%20%22America%2FNew_York%22%0A%20%20%3Alog-level%20%22info%22%0A%20%20%3Aenvironment%20%22local%22%7D&amp;schema=%5B%3Amap%20%0A%20%5B%3Alocale%20string%3F%5D%0A%20%5B%3Atimezone%20string%3F%5D%0A%20%5B%3Alog-level%20%5B%3Aenum%20%22trace%22%20%22debug%22%20%22info%22%20%22warn%22%20%22error%22%20%22fatal%22%5D%5D%0A%20%5B%3Aenvironment%20%5B%3Aenum%20%22local%22%20%22dev%22%20%22staging%22%20%22production%22%5D%5D%5D" rel="nofollow">https:&#x2F;&#x2F;malli.io&#x2F;?value=%7B%20%3Alocale%20%22en-US%22%0A%20%...</a>
nsmover 3 years ago
I’m not on board with using a language that only has a single implementation and is complex enough that implementing others is difficult.<p>Please use a language designed for configuration, but still sane. There are several if we leave the JSON-inspired family. Starlark, pystachio, Dhall. All are restricted, but allow functions and code reuse. All are simple enough to write a new interpreter for.
评论 #29255201 未加载
评论 #29255852 未加载
评论 #29256693 未加载
评论 #29257708 未加载
评论 #29261988 未加载
pramodbiligiriover 3 years ago
Anyone have experiences to share about Dhall (<a href="https:&#x2F;&#x2F;dhall-lang.org" rel="nofollow">https:&#x2F;&#x2F;dhall-lang.org</a>)?<p>On their homepage they call it a &quot;programmable configuration language that you can think of as: JSON + functions + types + imports&quot;
评论 #29256779 未加载
评论 #29256134 未加载
fay59over 3 years ago
Probably important to mention that at the moment, TypeScript as a configuration format means anyone who writes a configuration blob for you has arbitrary code execution. This is rarely an issue in practice, though.
评论 #29256435 未加载
评论 #29256201 未加载
评论 #29255858 未加载
s_gourichonover 3 years ago
Dhall seems to gather some praises, and not fall into the pits mentioned here. <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17523623" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17523623</a>
评论 #29255657 未加载
pphyschover 3 years ago
My money is on <a href="https:&#x2F;&#x2F;cuelang.org" rel="nofollow">https:&#x2F;&#x2F;cuelang.org</a> in this space.<p>As I understand it it&#x27;s essentially the Google Configuration Language (GCL) author responding to Hashicorp Configuration Language (HCL), with language development modeled after Golang. Still in early stages but very promising.
richeyryanover 3 years ago
I feel like the AWS CDK is a great example of this working well. Rather than puzzling over what config options are valid, I get autocomplete and compiler errors when I insist on using an incorrect option. You also get stuff like deprecation warnings showing up in your editor.<p>It also becomes trivial to parameterise config so I can setup the same general Cloudfront distribution once and tweak it based on the individual application. It&#x27;s open to abuse but it&#x27;s better than interpolating values into Cloudformation templates.
评论 #29255675 未加载
pstchover 3 years ago
Is evaluation lazy in TypeScript ?<p>For me, one of the most important features of a configuration language is the ability to directly refer to other values of the configuration, and more importantly, in the case of a configuration split in separate modules, to refer to values that could be provided by the other modules.<p>Essentially, this means that the final configuration should be the fixed point of a function, that the user would provide the configuration as a definition of this function (or in the case of multiple modules, as multiple definitions that would be merged using function composition), and that the evaluator would be able to determine the fixed point of this function.<p>I feel like this kind of configuration (used notably in NixOS) provides a lot of what&#x27;s missing in traditional configuration languages, makes it easier to compose configuration modules, without requiring to write the configuration in a Turing-complete language.
评论 #29255667 未加载
sirtimblyover 3 years ago
The line between configuration and code is always a little blurry. There are some projects where using a .js file for the &quot;configuration&quot; of another operation is incredibly useful. TS makes even more sense in many of those situations. Lots of configuration files require pre-processing already, like merging shell env vars in with textutil or something like that. So, I wouldn&#x27;t dismiss this opinion outright.
tantalorover 3 years ago
Using programming languages for configuration is a bad idea.
评论 #29255342 未加载
评论 #29257282 未加载
评论 #29255008 未加载
评论 #29256746 未加载
评论 #29260837 未加载
评论 #29260137 未加载
评论 #29260726 未加载
评论 #29259246 未加载
评论 #29254945 未加载
评论 #29254952 未加载
评论 #29255274 未加载
veidrover 3 years ago
I think this is kind of a no-brainer, <i>if</i> your project is already based on TypeScript.<p>Not just for configuration, but for things like test fixture data, and even data assets like i18n translations, or basically anything that projects might commonly use JSON for, internally. It&#x27;s just so much easier to create, edit, manage, and deal with in TypeScript.<p>But it is hard to imagine TypeScript-as-config taking root in projects that don&#x27;t already use TypeScript.
Slasher1337over 3 years ago
I&#x27;ve really come to prefer json5 as a config format: <a href="https:&#x2F;&#x2F;json5.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;json5.org&#x2F;</a><p>It&#x27;s basically Json with comments, trailing commas, and unquoted object keys (and some other minor things).<p>Much nicer than real json as a config format, but still simple and declarative, not requiring an interpreter or being turing-complete.
steeveover 3 years ago
Starlark or Cue, but not a general purpose language please. Side effects in a configuration language is an atrocity...
评论 #29257303 未加载
eb0laover 3 years ago
Really interesting but, sincerely, I would rather use sqlite.<p>I know: It&#x27;s a binary format. And you need to add a library to read it.<p><i>BUT</i><p>- Your configuration _has_ data types.<p>- You can have a copy with an old config within the file easily (create table backup_config as select * from actual_config; ).<p>- The file is small.<p>- Encryption is built-in if you need it.<p>- And it&#x27;s programable
eyelidlessnessover 3 years ago
I have used TypeScript for config in a few projects, with node-config[1], and it’s substantially better IME than JSON&#x2F;YAML&#x2F;etc. Yeah it’s unconventional to write configs in a general purpose language, and I understand why that would be undesirable. But having configs validated at compile time is great at preventing bugs.<p>I’m not sure I’d recommend node-config (it was chosen by a past team). Its magic undermines the confidence in type safety provided by choosing TypeScript in the first place. But it does give some good baseline conventions one can apply without all the magic.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;lorenwest&#x2F;node-config" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lorenwest&#x2F;node-config</a>
progreover 3 years ago
TypeScript needs to be compiled, right? So what use is a configuration file if I need to run it through the build pipeline?<p>The point of having <i>configuration</i> is to gain the ability to <i>configure</i> software without rebuilding in my mind. Maybe I&#x27;m missing something.
评论 #29259239 未加载
评论 #29255955 未加载
评论 #29255642 未加载
评论 #29255660 未加载
vander_elstover 3 years ago
What would be the problem with using something like protobufs? To write the configuration, you can use whatever language you want, what matters are the outputs anyway. The outputs are stored by automation in text format so that they can be better inspected. The programs that use such configuration have the advantage of knowing the schema of the configuration and directly parse it avoiding possible typing problems. The nice thing is that you can use the same language to write the programs and the configuration.
评论 #29257931 未加载
ThePhysicistover 3 years ago
I think parsing YAML or JSON into typed structures is the easier way to go. I e.g. do that in Golang using a little form validation and coercion library I&#x27;ve written. The end result is a nested, strongly typed data structure. Here&#x27;s an example: <a href="https:&#x2F;&#x2F;github.com&#x2F;iris-connect&#x2F;eps&#x2F;blob&#x2F;master&#x2F;settings.go" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;iris-connect&#x2F;eps&#x2F;blob&#x2F;master&#x2F;settings.go</a> (the accompanying form validation configuration: <a href="https:&#x2F;&#x2F;github.com&#x2F;iris-connect&#x2F;eps&#x2F;blob&#x2F;master&#x2F;forms&#x2F;settings.go#L78" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;iris-connect&#x2F;eps&#x2F;blob&#x2F;master&#x2F;forms&#x2F;settin...</a>). Hashicorp has some libraries that do similar stuff, I wrote my own though since I need it quite often and wanted to have full control over it. The library is just a few hundred lines of Golang code (<a href="https:&#x2F;&#x2F;github.com&#x2F;kiprotect&#x2F;go-helpers&#x2F;tree&#x2F;master&#x2F;forms" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kiprotect&#x2F;go-helpers&#x2F;tree&#x2F;master&#x2F;forms</a>). Undocumented as of now as it&#x27;s nothing I want to explicitly share with the world.<p>In my experience, a lot of the validation needs to be done at runtime anyway as type checking alone won&#x27;t allow you to e.g. validate if a string is a valid regular expression. Also, I think using TypeScript for configuration requires you to compile &amp; interpret the configuration file in order to check it and obtain the actual configuration values. Not sure if I like that as it requires bundling the entire TypeScript compiler with your program.
strogonoffover 3 years ago
TypeScript is limiting for describing schema in my experience. Try requiring an array of certain length, for example.<p>Also, it’s Turing-complete, so you are enabling future developers or users to shoot themselves in the foot and write something that does not halt already at configuration stage.<p>To me the perfect language for configuration seems to be Dhall, but it doesn’t seem to have gained any traction.
评论 #29255846 未加载
评论 #29256420 未加载
jayd16over 3 years ago
Kind of interesting but now I need to run arbitrary code to read a config?
评论 #29254966 未加载
enriqutoover 3 years ago
Typescript has a lot of punctuation and thus it&#x27;s not really human-editable. I&#x27;d prefer a ini-style format or dockerfile style (with one directive per line of text, clearly identified by a single uppercase word). Moreover, these formats are so easy to parse that you do not even need a library to do so.
评论 #29255638 未加载
评论 #29255876 未加载
taylodlover 3 years ago
You know what we don&#x27;t need? Yet Another Configuration Format! Also, don&#x27;t tell me I can&#x27;t add comments to JSON because I sure can - add it as a field! If a comment is that important then it&#x27;s important enough to transfer along with the dataset and durably persist.
评论 #29271783 未加载
fourseventyover 3 years ago
Personally I wouldn&#x27;t use Typescript for config. It breaks my #1 dev rule which is KISS. Keep it simple stupid. Simplicity is orders of magnitude more important than other things like DRY or config schema.
strifeyover 3 years ago
I&#x27;m curious what folks who agree with this think about setup.py then? There&#x27;s been a push to move to setup.cfg to specifically avoid the fact that python libraries run arbitrary code on install.
评论 #29258427 未加载
Shadonototraover 3 years ago
I can&#x27;t believe what i am reading...
评论 #29255601 未加载
ravenstineover 3 years ago
My only real criticism is that I believe it&#x27;s generally an anti-pattern to have configuration that necessitates DRY. It has essentially the same problems as OOP inheritance. Because configuration isn&#x27;t and <i>shouldn&#x27;t</i> be an application in and of itself, avoiding potential issues and misdirection from avoiding DRY in this case seems like the right tradeoff to having to use multi-select to change the config format in your IDE.
评论 #29262298 未加载
shadowgovtover 3 years ago
I like the idea of the IDE assisting with the schema-correctness of the config (which you get for free with TypeScript coupled with any competent IDE configured to use the typechecker to validate the code).<p>... but as a rule of thumb, if your config language is Turing complete, you&#x27;re setting yourself up for future pain as complexity of config grows with time and someone makes the bad decision to take advantage of that Turing completeness...
ReleaseCandidatover 3 years ago
Great idea. And because you need many constants that often change in such a configuration file you could put that all in another file and include that in the &#x27;real&#x27; configuration file. Why not call that, hmm, `config_config.txt`. But as plain text is a bit hard to parse, we could give that file some structure, so that it is like, well, another markup language, just without markup and easier to read and write.
vendiddyover 3 years ago
I think typescript as a config format alone may get too complex.<p>But json backed by a typescript interface definition could be a useful middle ground.<p>You&#x27;re still writing json but you get nice code completion and realtime validation of the config file.
sbr464over 3 years ago
I use TypeScript and like the ecosystem.<p>For broader use, I would like to see native support or an official extension for creating dependent types or specific constraints.<p>Things like number ranges, or the basics from JSON Schema, etc.
melolifeover 3 years ago
I&#x27;ve done a similar thing with Scala in the past - ship the compiler jar and then compile your configuration (which is just an abstract class implementation) as part of the startup process.
harunurhanover 3 years ago
Not typed support but I find jsonnet [0] pretty useful when you have tons of configuration files.<p>[0] - <a href="https:&#x2F;&#x2F;jsonnet.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jsonnet.org&#x2F;</a>
zffrover 3 years ago
Apple takes a similar approach for swift. Package.swift files run real swift code and export an object that specifies the package’s configuration.<p>In practice I think it works fairly well.
the_arunover 3 years ago
How to add comments in json or TypeScript config files? Commenting is very much needed for any manually written code or config we keep in repositories.
beebeepkaover 3 years ago
Typescript is perfect for stuff like this because it&#x27;s so much less verbose than most things out there<p>As a bonus, you might not need validation and schemas, too.
yayrover 3 years ago
Should this be a future ECMAScript enhancement or is the better route for browsers to natively support TypeScript?
intellixover 3 years ago
YAML is beautiful when it&#x27;s simple and horrible when you need to dynamically generate it. The indentation...
whirlwinover 3 years ago
The idea of allowing logic from a programming language in a config format is.... at best very horrifying...
emodendroketover 3 years ago
OK, so we&#x27;re coming back around to the &quot;configuration as code&quot; trend again then.
nlyover 3 years ago
Protocol Buffers text format ?
评论 #29257276 未加载
评论 #29256796 未加载
11235813213455over 3 years ago
or json-schema with jsonc or js files