typescript

This magazine is from a federated server and may be incomplete. Browse more on the original instance.

tyler, in Google TypeScript Style Guide

Google’s java style guide is terrible, even though it’s pretty much standard. not sure why I’d want to trust their typescript style guide.

lysdexic,

Any style guide is better than no style guide. Until a better option is presented, something is better than nothing.

sloppy_diffuser, in Writing exhaustive switch statements in TypeScript

There is support for this in Effect as another implementation.

effect.website/docs/guides/…/pattern-matching

towerful, in Writing exhaustive switch statements in TypeScript

Just have default throw an error? Then, if you add a value to the string union (hopefully) your tests will highlight it isnt handled.

Equally, for such a simple switch, surely a map is better?
But i rarely find myself using switches.
I find ifs with early returns cleaner. Allows for variable creation during execution (which my tslint complains about)

sbv,

I really appreciate compile errors in this scenario. In Rust the compiler will freak out when there are missing enum variants in a match. Tests are great, but compiler errors don’t rely on exhaustive and properly written tests.

arendjr,

Generally, I would recommend against throwing in the default case, since it will actually reduce your type safety (exceptions bypass all your other types). But I learned that with another clever trick this can be avoided, giving both runtime and compile time safety: exploringjs.com/tackling-ts/ch_enums.html#exhaust…

lorefnon, in DBOS Cloud: A transactional serverless computing platform with first-class TypeScript environment

A big caveat looks to be that paid plan pricing is not disclosed. So likely geared towards enterprise customers.

fidodo, in How to Do a JS to TypeScript Conversion

The loose approach could be good as a way to get your feet wet if the entire team has no preexisting TS experience and needs to get weaned into it.

However when it comes to seriously converting the codebase I completely agree, not just for TS conversations, but for all major refactors. Slowly updating the same file over and over again is simply inefficient and leads to things never fully getting done.

Gentoo1337, in overridable-fn: lightweight IoC/DI solution - Functions whose behavior can be swapped at runtime
@Gentoo1337@sh.itjust.works avatar

Imagine the runtime errors

SatouKazuma,
@SatouKazuma@lemmy.world avatar

This sounds like a walking runtime error just waiting to happen. Or a litany of them, if you’d so prefer. Debugging would be a nightmare and a half.

lorefnon,

Hi - I have been using this in production for a few months now, and haven’t experienced significant issues with debugging or troubleshooting.

It also helps that the library is type-safe - if we are overriding a function, we must comply with the signature of the original function for TS to pass.

Wrt. run time errors, the use of proxy does not interfere with good stack traces, if that is what you are worried about.

If we take the example in this gist where an overriden function throws an exception, we can see that the stack trace clearly shows the file (which overrides the hello function) where the exception is originating from.

In effect, the DX is not much different if you use any other mechanism for making some feature runtime configurable. In comparision to most traditional IoC/DI utilities I find the DX to actually be superior:

  • Unlike a lot of other IoC solutions whose APIs are cloned/inspired from similar Java/C# libraries, you don’t need to wrap things in classes to participate in the DI system
  • Also, that a function is overridable is transparent from consumer side - they just call a function that they import without needing any @Inject/@Provide boilerplate. This is great if you are writing a library or a small utility and don’t want to enforce usage of a specific DI/IoC library for your consumers.

What we don’t have (and likely will never) is support for things like request scoped DI or hierarchy of IoC containers. In mostly-functional TS code I have seldom missed those patterns.

not_woody_shaw, in typia

Looks good so far. How do I get it to play nice with jest / ts-jest ?

not_woody_shaw,

Oh, I get it. It’s like a tech demo, to encourage Microsoft to add compiler plugins. Can’t really use it on a project of any scale, due to every other tool also providing its own TS compiler, again because TSC doesn’t support plugins.

lorefnon,

I use it in the code generator mode (typia.io/docs/setup/#generation) that doesn’t require the tsc plugin integration and works well with other build tools (in my case esbuild) and arbitrary testing/code-coverage libraries.

not_woody_shaw,

Oh, you just add it as a separate task in package.json? That seems nicer than converting all your type defs manually to io.ts format or similar.

lorefnon,

Yes, that mode works pretty well. In my case I don’t really own the types (they come from a third party source) so being able to to auto generate validators from them is very convenient.

thepiggz, in OO awkward

GPT4 rewrite:

Title: Optimizing TypeScript: Embracing Functional Patterns and Leveraging V8’s Strengths

Body:

Hello TypeScript Community! Our journey in TypeScript development benefits greatly from a deeper understanding of JavaScript’s core mechanics and the V8 engine’s optimizations. It’s become increasingly clear that JavaScript, and by extension TypeScript, subtly discourages traditional object-oriented (OO) programming in its built-in functionalities, favoring functional programming paradigms instead. Combining this with V8’s optimization techniques can significantly enhance our code’s performance and readability.

  1. Functional Over OO in JavaScript’s Built-ins:

    • Function References in Core Functions: JavaScript’s built-ins, like addEventListener, setTimeout, setInterval, and array methods (map, filter, reduce), prefer function references over objects with expected methods. This design choice subtly favors functional programming over OO.
    • Closures for State Management: Instead of passing around shaped objects or using bind, closures provide a more natural and efficient way to maintain state in functions, aligning well with JavaScript’s functional tendencies.
  2. Understanding and Utilizing V8’s Optimizations:

    • Leveraging Hidden Classes and Inline Caching: Consistent object property initialization helps V8 create and reuse hidden classes efficiently, optimizing property access. Inline caching improves performance by optimizing repeated property and method access on objects.
    • Best Practices for V8 Optimizations: Avoid dynamic changes to object shapes and focus on writing monomorphic code to aid V8’s optimization processes.
  3. Adapting to Functional Programming in TypeScript:

    • Functional Patterns for Performance: Embrace functional patterns, such as immutable data and pure functions, which V8 optimizes more effectively than complex class hierarchies.
    • Using TypeScript Features for Functional Code: TypeScript’s powerful type system, including interfaces and type aliases, supports a more functional style of coding, which is inherently more aligned with V8’s optimization strategies.
  4. Moving Away from Classic OO Patterns:

    • Challenges with OO in TypeScript: The behavior of this, complex inheritance structures, and the awkwardness of bind highlight the incongruities of OO patterns in TypeScript.
    • Functional Techniques as Solutions: Opt for closures and composition over inheritance. Utilize functional programming constructs to create more modular, reusable, and efficient code.

In conclusion, by understanding the inherent functional leanings of JavaScript and TypeScript, and by aligning our coding practices with V8’s optimization techniques, we can write more efficient and maintainable TypeScript code. This approach not only capitalizes on the language’s strengths but also ensures our code is primed for optimal performance by the V8 engine. Let’s continue to explore and embrace these functional paradigms and share our insights on leveraging V8’s capabilities in TypeScript development.

HumanBehaviorByBjork, in TypeScript's Hidden Feature: Subtypes
@HumanBehaviorByBjork@hexbear.net avatar
towerful, in TypeScript's Hidden Feature: Subtypes

I presume the details of this post got lost somewhere?


<span style="color:#323232;">let x = () => ({ name: "Alice" });
</span><span style="color:#323232;">let y = () => ({ name: "Alice", location: "Seattle" });
</span><span style="color:#323232;">x = y; // OK
</span><span style="color:#323232;">y = x; // Error, because x() lacks a location property`
</span>

www.typescriptlang.org/…/type-compatibility.html

There doesn’t seem to be an article directly on subtypes

Jimbabwe,

Typescript error in Post. Was expecting argument of type Link, received undefined.

Vilian, in $20k Bounty was Claimed! · Prettier

“we don’t have competition, so let’s do it ourself and sponsor it”

lmao, powermove

Armaell, in Announcing TypeScript 5.3 - TypeScript

Nothing exciting, but QoL is always welcomed

victorz,

I was excited!

Statick, in So, are enums ok or not?

At work, when I was helping with some frontend stuff, we used object literals.


<span style="color:#323232;">const DIRECTIONS = {
</span><span style="color:#323232;">  UP: "UP",
</span><span style="color:#323232;">  DOWN: "DOWN"
</span><span style="color:#323232;">} as const;
</span><span style="color:#323232;">
</span><span style="color:#323232;">type DIRECTIONS = typeof DIRECTIONS[keyof typeof DIRECTIONS];
</span>

Taken from option 2 in this blog post. …medium.com/alternatives-to-typescript-enums-50e4…___

Fisherswamp,

This is, in my opinion, the absolute best way to do it

fidodo,

That’s what the typescript official docs recommend as an alternative too

www.typescriptlang.org/docs/handbook/enums.html#o…

The const keyword was added after enums. I doubt enums would exist if the feature was added earlier.

Aux, in So, are enums ok or not?

Use enums when you need enums. Don’t use them when you don’t need them. What’s the issue?

fidodo,

You probably don’t ever need enums since the const keyword was added after enums and const objects handle enum use cases unions don’t.

Aux,

You mean const assertions. Well, the thing is that const assertions are not enums. They don’t handle enum use cases in any way.

fidodo,

I’m talking about if you want an object syntax for accessing constant types to act as an enum www.typescriptlang.org/docs/handbook/enums.html#o…

spartanatreyu, in So, are enums ok or not?
@spartanatreyu@programming.dev avatar

Basically, you can use them if you want to, but every single case where you would use them would be better served with a union type.

pm_me_your_quackers,

To add, turning everything into an enum can make the code nearly unreadable and hard to work with. This isn’t Rust, and there’s no performance gain for using enums over string unions.

LeafOnTheWind,

Or by an enum class.

fidodo,

Another good option is const objects, it’s a recommended alternative in the official docs. www.typescriptlang.org/docs/handbook/enums.html#o…

  • All
  • Subscribed
  • Moderated
  • Favorites
  • typescript@programming.dev
  • DreamBathrooms
  • ngwrru68w68
  • cubers
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • tacticalgear
  • JUstTest
  • osvaldo12
  • normalnudes
  • tester
  • cisconetworking
  • everett
  • GTA5RPClips
  • ethstaker
  • anitta
  • Leos
  • provamag3
  • modclub
  • megavids
  • lostlight
  • All magazines