DEV Community

loading...

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

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.