4 ms·
Learn infer and extends and you will accept all the downsides being zen from the height of your highly well typed codebase. If you don't find the following comp
by ly3xqhl8g9 4y ago
Learn infer and extends and you will accept all the downsides being zen from the height of your highly well typed codebase. If you don't find the following compelling:
type IsParameter<Part> = Part extends `[${infer ParamName}]` ? ParamName : never;
type FilteredParts<Path> = Path extends `${infer PartA}/${infer PartB}`
? IsParameter<PartA> | FilteredParts<PartB>
: IsParameter<Path>;
type ParamValue<Key> = Key extends `...${infer Anything}` ? string[] : number;
type RemovePrefixDots<Key> = Key extends `...${infer Name}` ? Name : Key;
type Params<Path> = {
[Key in FilteredParts<Path> as RemovePrefixDots<Key>]: ParamValue<Key>;
};
type CallbackFn<Path> = (req: { params: Params<Path> }) => void;
function get<Path extends string>(path: Path, callback: CallbackFn<Path>) {
// TODO: implement
}
get(
'/purchase/[shopid]/[itemid]/args/[...args]',
({ params }) => {
params.shopid // number
params.itemid // number
params.args // string[]
},
);
(https://lihautan.com/extract-parameters-type-from-string-literal-types-with-typescript https://lihautan.com/extract-parameters-type-from-string-lit...)
then you probably don't need TypeScript, or types in general. And that is fine.
As to the points:
(i) 99.99% of everything is being developed by someone else in our current stack, but here comes the benefits of open source: don't like how a project is going? fork it and maintain it yourself;
(ii) If you don't use a NPM package with the last release older than 2014 (and why would you given your (i)st fear), chances are pretty high (95%+) the library is written directly in TypeScript or there is a DefinitelyTyped package with the types;
(iii) Type errors, those that come directly from TypeScript, are pretty straightforward (was expecting X, you passed Y);
(iv) Yes, TypeScript should have never been written in an interpreted language, but what were the options in 2012: C? C++? Maybe some day in the unspecified future, we will have a Rust/compiled language implementation with microsecond transpilation time.
- throwaway2037 4y agoDid you write that blog post? Cripes, it is amazing. What a brilliant idea. Thank you to share. My brain is wired for C++ templates, and the technique makes good sense! To your last point: Why not C#? It is interesting did not mention it, as TypeScript came from Microsoft R&D (as I understand).
- ly3xqhl8g9 4y agoI did not write the blog post, it was just a simple infer/extends non-trivial example explained in depth, commonly used in packages like express and so forth. TDungeon [1], a game running on the TypeScript type system, really showcases how powerful the type system truly is. Why not C#? Probably because Anders Hejlsberg [2], lead architect of C# and core developer of TypeScript, wanted something different. More of a sociological, rather than technological, reason. [1] https://github.com/cassiozen/TDungeon https://github.com/cassiozen/TDungeon [2] https://en.wikipedia.org/wiki/Anders_Hejlsberg https://en.wikipedia.org/wiki/Anders_Hejlsberg
- tobyhinloopen 4y agoOn the one hand, I like this very much. On the other hand, if I open a project and I see many lines of complex types that don't do anything and I have to understand these types to use them to fix some bug, I'm going to say words out loud that I cannot write here. I've seen projects where there were many lines of complex types and, after updating some dependencies, there were many type errors even though the project ran fine and the tests were all green. Pisses me off every time. So in the end, I rather just see `params` be `unknown` and create a function to extract typed values, like `getNumberParam(params, 'shopid')` that returns a number or throws some kind of error. That way, you can compile-time checks AND run-time checks.
- ly3xqhl8g9 4y agoAs always, it's a problem of balancing complexity vs. utility while striving to raise the level of abstraction. With regards to TypeScript, any, unknown, never are perfectly valid types and plenty useful, just need to be used appropriately.