TypeScript

821 readers
1 users here now

founded 1 year ago
MODERATORS
26
7
Announcing TypeScript 5.3 RC (devblogs.microsoft.com)
submitted 11 months ago by mac to c/typescript
27
28
 
 

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?

29
30
31
32
33
17
submitted 1 year ago* (last edited 1 year ago) by philnash to c/typescript
 
 

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

34
12
submitted 1 year ago by mac to c/typescript
35
7
TypeScript 5.3 First Look (www.totaltypescript.com)
submitted 1 year ago by mac to c/typescript
36
 
 

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

37
38
 
 

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.

39
 
 

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.

40
20
submitted 1 year ago by danhab99 to c/typescript
41
11
submitted 1 year ago by mac to c/typescript
42
 
 

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.

43
 
 

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.

44
45
18
Announcing TypeScript 5.2 Beta (devblogs.microsoft.com)
submitted 1 year ago by jmondi to c/typescript
46
7
hello (lemmy.douwes.co.uk)
submitted 1 year ago by [email protected] to c/typescript
 
 

posting for mod