TypeScript

14 readers
2 users here now

founded 1 year ago
MODERATORS
26
27
 
 

I have googled this and even got to the second page, and there does not seem to be any kind of consensus.

As far as I can tell, it's a bad idea because it creates code you don't see and accepts inputs that you wouldn't want. And yet, many people seem to like them more than, say, const unions due to being easier to refactor in bulk.

So what gives? Is this a case of IT people having very strong opinions on stuff that doesn't matter? Or is there a technical reason for or against it?

28
29
30
31
 
 

A minimal typescript utility to combine multiple collections (possibly from different sources) into a single hierarchy - in a fully type-safe manner.

32
17
submitted 11 months ago* (last edited 11 months ago) by [email protected] to c/[email protected]
 
 

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

33
34
35
 
 

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

36
37
 
 

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.

38
 
 

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.

39
40
41
 
 

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.

42
43
44
3
hello (lemmy.douwes.co.uk)
submitted 1 year ago by [email protected] to c/[email protected]
 
 

posting for mod