DEV Community

loading...

Discussion on: TypeScript is slow. What can we do about it?

Collapse
joelbonetr profile image
JoelBonetR

Can you, please share your laptop specs for adding context?
I developed with TS using a shitty i5 8250u and 16Gb RAM, also tried with a R5 3500u with 8Gb RAM and it was fine, next week I'll try with my new office laptop which is an i7 1065G7 with 16Gb RAM and I'll see if there's any visible improvement

Collapse
alekseiberezkin profile image
Aleksei Berezkin Author

It's good but usual MacBook Pro with i7 and 16 GB RAM. Most of the time it's fine; interesting things begin when the file size approaches 1,000 lines and heavily uses type inference. Funny is that not specifying types and letting a compiler figure them out is somethings we like TS for 😀

Collapse
sharpninja profile image
The Sharp Ninja

Not specifying types is an anti-pattern and the root of your problem.

Thread Thread
alekseiberezkin profile image
Aleksei Berezkin Author

Could you please elaborate a bit on this? Because I actually heard the opposite.

The Effective TypeScript by Dan Vanderkam discusses this in details:

Chapter 3. Type Inference

Item 19. Avoid Cluttering Your Code With Inferrable Types

(...) in TypeScript many annotations are unnecessary. Declaring types for all your variables is counterproductive and is considered poor style.
(...)
Ideal TypeScript code includes type annotations for function/method signatures but not for the local variables created in their bodies.

Things to remember

  • Avoid writing type annotations when TypeScript can infer the same type (...)

I agree that this may be the place where the beautiful theory meets the harsh reality. But anyway, type inference is something every language is sort of proud of, and one of their promotion points. And keeping away from this feature looks like artificially limiting your expressiveness.

Thread Thread
sharpninja profile image
The Sharp Ninja

I hate terseness for the sake of terseness. Yes, many languages can infer type at compile time, but as you can see from your own experience is that there's a huge cognitive load on the toolset to do so. Now, think about a human reading your code and having to apply the same type of cognitive load to infer types so they can effectively maintain your code.

Thread Thread
alekseiberezkin profile image
Aleksei Berezkin Author

I think that may be a matter of measure. And taste to some extent. Best practices have some exceptions; or these are the part of a best practice. However, I doubt you like an example without a single inferred type.

// 3 types inferred: upper, i, output of map
const upper = (a: string[]) => a.map(i => i.toUpperCase())

// 3 types inferred: a, i, map output
// This is recommended in Effective Typescript
const upper: (a: string[]) => string[] =
    a => a.map(i => i.toUpperCase())

// All types are explicit
const upper: (a: string[]) => string[] =
    (a: string[]) => a.map<string>((i: string) => i.toUpperCase())
Thread Thread
sharpninja profile image
The Sharp Ninja
// All types are explicit
const upper: (a: string[]) => string[] =
    (a: string[]) => a.map<string>((i: string) => i.toUpperCase())

or

Func<IEnumerable<string>, string[]> Upper = 
    a => a.Select(i => i.ToUpper());

With C# the compiler only needs one type specification and then the IDE simply knows the variable's type because it cannot change and thus doesn't have to be inferred at each usage. The single strongly typed Func<> declaration has all the types defined for the lambda.

Thread Thread
alekseiberezkin profile image
Aleksei Berezkin Author

I don't get your point. You don't like type inference in TS but like it in C#?

Thread Thread
sharpninja profile image
The Sharp Ninja

It's not inference in C#. The types are known at declaration and cannot change. The issue that the original article is about is a side-effect of TypeScript constantly trying to enforce types when none actually exist. Strong typing in TS is an illusion, and the only way to prop up that illusion is for the language server to constantly calculate the object graph's types so it can notify of illegal assignments.

Thread Thread
alekseiberezkin profile image
Aleksei Berezkin Author

Knowing types from a context is not a type inference? Whut? 😲 Then what is type inference, if not this? How does editor “know” that i in your C# example is a string? Compiler (or editor plugin, the same) infers its type for you. To achieve this, it constantly monitors all changes (just like TS server), and restarts the process whenever something is changed (just like TS server). Otherwise how this would work do you think? All languages work the same. For TS there are certain things which make it slow, and this can be improved — that was my point.

Thread Thread
sharpninja profile image
The Sharp Ninja

There is no need to check a type unless it's declaration changes or it is being referenced as a different type than its declaration. In all other cases there's nothing to infer, the IDE already has the correct type for the object. With TypeScript you can disable strict typing for object that are implicitly typed as Any and thus create a situation where the IDE cannot know the type and must ask the language server to infer the type, which will probably come back as Any.

Thread Thread
alekseiberezkin profile image
Aleksei Berezkin Author

any works the opposite way 😀 any tells TS and IDE to stop inferring or checking a type of a var. Thus, if your code is full of anys, compilation will be the fastest. In my example there's not a single any, all types are strict.

Collapse
tominekan profile image
Tomi Adenekan

i5 is sh**ty? Try working with Intel celeron and 4 gigs RAM. 😂