TypeScript

798 readers
1 users here now

founded 1 year ago
MODERATORS
26
27
28
29
30
 
 

Click bait title, but this post goes into depth about using tsconfig.json correctly and across different layers of your project.

31
32
1
TypeScript 5.3 First Look (www.totaltypescript.com)
submitted 11 months ago by [email protected] to c/[email protected]
33
 
 

Working with the primary TS compiler API is pretty arduous. It is nice to see dedicated community effort towards making this more approachable.

34
35
 
 

I've looked into these three and they all seem very similar and seem to cover the same use cases. Does anyone have experience with them? I'm having a hard time making a decision or even figuring out the pros and cons of each of them.

36
 
 

i made a type-safe GroupBy function.

/**
 * Groups array of objects by a given key
 * @param arr array of objects to group
 * @param key must be present on every object, and it's values must be string|number
 * @author telepresence
 * @license CC-BY-4.0
 */
function groupBy(arr: T[], key: keyof T, defaultAcc: Record = {}) {
    return arr.reduce((acc, val, i) => {
        const compValue = val[key];
        if (typeof compValue !== 'string' && typeof compValue !== 'number') {
            throw new Error(`key ${key.toString()} has values other than string/number. can only group by string/number values`);
        }
        if (!acc[compValue]) acc[compValue] = []
        acc[compValue].push(val);
        return acc;
    }, defaultAcc);
}
  • like lodash's groupBy, but by key and not function
    • group an array of objects which all have a key in common into an object with keys matching all the different possible values of your common key
  • type-safe, no unknown's no any's
  • does not copy arrays ([...array]), uses push
  • supports selecting by keys, where the key values are string / number (although you can easily add symbol support)
  • shared for free under the CC BY 4.0 license - only attribution is requred (link to this post is fine)
  • custom default accumulator support, if you already know the groups beforehand and would rather have an empty array than undefined.

example:

const data = [{
  "name": "jim",
  "color": "blue",
  "age": "22"
}, {
  "name": "Sam",
  "color": "blue",
  "age": "33"
}, {
  "name": "eddie",
  "color": "green",
  "age": "77"
}];

groupBy(data, 'color') 

would result into:

{
  "blue": [
    {
      "name": "jim",
      "color": "blue",
      "age": "22"
    },
    {
      "name": "Sam",
      "color": "blue",
      "age": "33"
    }
  ],
  "green": [
    {
      "name": "eddie",
      "color": "green",
      "age": "77"
    }
  ]
} 

TL;DR i've sucessfully wrote something using generics in typescript for the first time, and i think it's pretty epic.

37
38
39
 
 

Why I think Typescript is A Good Thing, But mainly a complaint about the number of programming languages. Other than objects (which were not invented), I don't think any of them are a marked improvement on PL/1.

40
 
 

I came up with a trick to iterate over string union types (kind of). I don't know if anyone has discovered this independently, but I think it's too useful not to share.

You can do it with a pattern like this:

const values = ['foo', 'bar', 'baz'] as const;

type Value = (typeof values)[number];

Let's break down what is going on here. values is a list of strings, however the as const makes it so that Typescript treats it as a tuple of string literals instead of just string[]. When defining Value, we use that tuple type to get the type of what you would get by indexing the tuple with a number. Since that type could be any of the string literals in the tuple, the resulting type is a union of all the literals in the tuple, i.e. 'foo' | 'bar' | 'baz'. This makes it so you can use Value as the union type, and if you ever want to iterate over all the values of the union, you can do that with values. This should allow you to do the one thing that Typescript's crappy enums has over string unions.

You can see this technique in action in the bot library I made.

41
42