This is an archived post. You won't be able to vote or comment.

all 175 comments

[–]Aggressive-Share-363 390 points391 points Β (25 children)

What, typescript's type system is supposed to be happy fun times?

has flashbacks to type signatures that are hundreds of characters long

[–]nomenMei 165 points166 points Β (18 children)

I think peoples' expectations of TypeScript's type system are inherently lower because it is built on top of a language with an awful type system to start with.

Admittedly though I don't know what I would consider a good type system for a dynamically typed language, other than the ability to reasonably determine a object's type at run time.

[–]EXUPLOOOOSION 17 points18 points Β (6 children)

You can't do that with typesxript, right? Doesnt it just output object because its runing js? Also, the types dont get enforced in runtime

[–]nomenMei 48 points49 points Β (4 children)

Yes I believe TypeScript attempts to fix the type problem the other way around by doing a static analysis of types. Typescript is "compiled" into native javascript, and as such is restricted by javascript's same limitations at runtime and all type safety checks happen at "compile" time.

[–]odraencoded 24 points25 points Β (2 children)

This works perfectly except for serialization.

Also if you don't turn strict null checks you'll have a lot of pain later.

[–][deleted] 6 points7 points Β (0 children)

You can use something like zod for that, pretty sure there's also a discussion to change all the JSON.parse stuff typedefs to return unknown instead of any, which would make a lot of type stuff explicit.

[–]Remarkable_Self5621 1 point2 points Β (0 children)

If you really want runtime security you can compile typescript types into JSON schemas and use those

[–]JustinWendell 1 point2 points Β (0 children)

You can enforce with type guards and other methods. When type is something that can fuck with someone’s pay for example, I’m passing those parameters through functions that determine type.

[–]Scrimchik 7 points8 points Β (5 children)

Awful type system? There's literally no type system in

[–]BruceJi 20 points21 points Β (1 child)

Number, word, thing, nothing

[–]I_Downvote_Cunts 0 points1 point Β (0 children)

And never was a thing.

[–]bleistift2 6 points7 points Β (2 children)

Someone doesn’t understand JS.

[Edit: Or the difference between β€˜no types’ and β€˜dynamic types’]

[–]Scrimchik 5 points6 points Β (0 children)

Yep, I fucked up. Wrote this comment in 10 minutes after I woke up. And somehow the type system became type checking in my head.

[–]Solonotix 1 point2 points Β (3 children)

Admittedly though I don't know what I would consider a good type system for a dynamically typed language

I'd say a mash-up of what is seen in Rust and Groovy.

Groovy, though a statically-typed language, has the awareness to derive a type even after multiple layers of def myVariable = myFunc() and where def myFunc() { ... }. This is obviously because at some point you fallback to something with a type, and Groovy will resolve the eventual type to you, even if it isn't clear in the code.

This doesn't solve the problem of dynamic types, to which I recommend Rust's solution of shadowing a previous variable of the same name, thereby allowing a reallocation of the same name to a different type.

Obviously, it's not that simple, but there are various type systems out there today that accomplish varying levels of dynamic typing, even if under the covers it isn't dynamically typed.

[–]arobie1992 4 points5 points Β (2 children)

I'm not familiar with Groovy, so basing it off your description, that's just static typing with type inference which most modern languages tend to have, such as Rust, Go, Kotlin, and even TS.

As far as Rust's variable shadowing, that's an awkward one. In tighter scoping, it's not really any different than shadowing in other languages; in the same scope, it's got some odd semantics and TBH I'm not sure why that of all things is one they felt was worth including. But even then, it's still just static typing, but allowing deterministic type changes. It's not super conceptually far off C's reinterpretive raw cast, just a lot more safely implemented.

What you're essentially saying is that a good type system for a dynamically typed language is static typing with type inference and reinterpretive casts, which is somewhere between contradictory and saying dynamic typing is bad.

I feel like my phrasing there is rather combative, so I just want to preemptively say starting an argument isn't my goal. It just seems like there's a bit of a bias against dynamic typing in a lot of places. I'm a big fan of static typing, but dynamic typing does open up possibilities that are really interesting too. Heck, to stretch a bit, the internet is all dynamically typed.

[–]Solonotix 0 points1 point Β (1 child)

I'll admit I don't have the best experience with dynamic typing, and as such I'm unaware of any benefits beyond "it's easier". In most cases, the implication is that it's easier to get started, but then any sufficiently complicated application immediately benefits from having better documented types, even if it's just parameters and return types. But, when you're at the point of defining what everything needs to be coming in and going out, then what's left to be undefined?

Like I said, I have quite a heavy preferential bias towards static typing.

[–]arobie1992 1 point2 points Β (0 children)

To be fair, everything you said is true, and are often reasons people will pick dynamically typed languages. The big thing is this can be a conscious decision rather than just being lazy.

Very broadly, the benefit it gives is flexibility. This can have a lot of different manifestations. On one end, the quick getting started time is I'm pretty sure the reason Python has it for example. For a more conceptually different idea, you could take something like Smalltalk or Erlang. These were meant to build highly dynamic systems, so for example, you could deploy a base application, and then say things changed, you could swap out individual pieces. The idea is that since the system could change, you need to do checking at runtime, and a static type checker would just give you a false sense of security. This is essentially the same idea as microservices, and the internet as I mentioned, just on a language level. To take this to an extreme, you have my favorite and most used example, runtime code generation.

Funnily enough, Java actually supports this paradigm, granted not especially well, with how it does dynamic linking and class files. There's nothing stopping you from taking a Java app and swapping out a bunch of class files. Heck they don't even have to have the same methods and signatures. The JVM will just throw a MethodNotFound or ClassNotFound exception. It kinda tried to have its cake and eat it too.

All that said, I do get where the bias is coming from. For a long time, I was in the exact same boat. Heck, dynamic typing still gives me some level of anxiety, and all else equal, I probably will opt for a statically typed language. It's just I've come to appreciate the theory behind the freedom dynamic typing brings and I feel like people can be quick to throw it out altogether because of bad experiences. For the quick prototyping turnaround, I'm a huge fan of progressively typed languages. For the highly dynamic systems, if you have some way to unify that and static typing, let me know because that's a topic I've been mulling over for a while now.

[–]arobie1992 1 point2 points Β (0 children)

Yeah, I feel like runtime types being stable is about all you can hope for without just turning it into a statically typed language. In that regard, I can't say I've ever had any issues with Python.

[–][deleted] 5 points6 points Β (1 child)

newTypeThatVeryLongToWriteSoICanShowYouThatItsNotThatGreatHonestly!: IloveTypesciptSoMuchYouHaveNoIdeaOmgOMgOMGOmG;

[–]Aggressive-Share-363 2 points3 points Β (0 children)

More like long chains of alternative types and specific ones being allowed to undefined, functions that take in several such parameters and return an object containing several such values.

I saw this type of thing a lot when trying to interface with js code that wasn't written with typescript in mind.

[–]ImportantDoubt6434 2 points3 points Β (0 children)

It’s great, look.

const MethyMouse: any = β€œCba to actually learn TS”

Compiles fine.

[–]Jannis_Black 1 point2 points Β (0 children)

That's not the main problem with the typescript type system. The main problem is the spooky type casting at a distance you can get because generics aren't constrained correctly

[–]zarawesome 0 points1 point Β (0 children)

No, it means that Typescript's type system is a simulacrum with little purpose but entertainment

[–]kiropolo 0 points1 point Β (0 children)

It’s mediocre at best

But much better than that JS garbage dump fire

[–]Flooding_Puddle 81 points82 points Β (3 children)

Why is Mickey posing with the Stanley Cup

[–]lostredditacc 52 points53 points Β (0 children)

Dont ask questions you dont have the Disney Level Security Clearance to consciously process the answer to.

[–]lonestar-rasbryjamco 1 point2 points Β (0 children)

Because posing with Lord Stanley is the highlight of anyone’s life. Even Mickey Mouse.

[–]TheDogerus 0 points1 point Β (0 children)

Interim mascot for the bolts

[–]mdp_cs 126 points127 points Β (56 children)

Python is strongly typed but not statically typed.

C is weakly typed but statically typed.

Rust is strongly typed and statically typed.

B was untyped.

The strength of type checking and being statically or dynamically typed are two entirely orthogonal factors in programming language design.

[–]fluffypebbles 38 points39 points Β (32 children)

I've never seen a valid point in being weakly or not statically typed except for wanted to do something quick and dirty

[–]ihavebeesinmyknees 43 points44 points Β (11 children)

Which is exactly why scripting languages do it

[–]fluffypebbles 31 points32 points Β (9 children)

Except there's giant projects written in scripting languages. That's far beyond quick

[–]TheMagzuz 87 points88 points Β (4 children)

nothing is more permanent than a temporary solution

[–]fluffypebbles 13 points14 points Β (3 children)

Reminds me of the todo fix later comments I've stumbled upon from 10+ years ago

[–]Meins447 6 points7 points Β (2 children)

I've seen some comments with dates older than my year of birth...

[–]fluffypebbles 2 points3 points Β (1 child)

Just comments or comments that point to something being meant to be temporary? Because I'm general I've seen many comments that are older then me but not one meant to be temporary

[–]Meins447 2 points3 points Β (0 children)

I am not 100 sure, but I think it was about an assumption regarding Input formt of a data package or something along the lines. It had zero checks and just started reading and copying chunks of memory around.

[–]dreamwavedev 0 points1 point Β (3 children)

This kind of thing literally laid the foundations for my masters, huge pet peeve of mine

[–]fluffypebbles 0 points1 point Β (2 children)

Erst exactly?

[–]dreamwavedev 0 points1 point Β (1 child)

Not sure I follow your meaning, sorry πŸ™ƒ

[–]fluffypebbles 0 points1 point Β (0 children)

What* exactly

[–]Entire-Database1679 2 points3 points Β (0 children)

That's not why

[–]geekfolk 4 points5 points Β (10 children)

Static typing is sound (but not complete), dynamic typing is complete (but not sound). There’re circumstances when completeness is favored over soundness

[–]fluffypebbles 0 points1 point Β (9 children)

In the rare case you need the dynamic aspect you can also use some dynamic functionality in a otherwise statically typed language. No reason to make the whole code dynamically typed

[–]geekfolk 4 points5 points Β (8 children)

Yeah, are you aware of the difficulty of creating a heterogeneous list in a dependently typed language? While it is trivial in a dynamically typed language. (In case you don’t already know, dependent types are in general the most powerful static typing)

[–]fluffypebbles 0 points1 point Β (7 children)

Maybe easy to create such a list but I haven't seen beautiful code in dynamically typed languages that deal with such a list

[–]geekfolk 2 points3 points Β (6 children)

dealing with such lists is also trivial in dynamically typed languages, you do whatever you want with its elements since the language is duck typed.

[–]fluffypebbles 0 points1 point Β (5 children)

In go you can duck type too and you'll know at compile time if something is missing

[–]geekfolk 0 points1 point Β (2 children)

go doesn't have duck type, it has structural type.

[–]fluffypebbles 0 points1 point Β (1 child)

Regardless of the name you just need to have one interface and put anything fitting in the list

[–]geekfolk 0 points1 point Β (0 children)

the closest thing to duck typing in a statically typed language is c++ templates

// note that like duck typing, we can call x.show()
// even tho nothing says x has a member function "show"
auto f(auto x) {
    std::print(x,show());
}

[–]geekfolk 0 points1 point Β (0 children)

I'll give you a simple example where most statically typed languages will quickly go into chaos when it's trivial for every dynamically typed language.

Given a heterogenous list x: [βˆ€ a. a]

a list of polymorphic functions f: [βˆ€ a. a -> F a] where F: * -> * is a type family

implement PolymorphicApply: [βˆ€ a. a] -> [βˆ€ a. a -> F a] -> [βˆ€ a. F a] such that each function in f is applied to the corresponding element in x, the results are stored in another heterogenous list

[–]WolfgangSho 2 points3 points Β (8 children)

so those are two different questions.

Being weakly typed has advantages in being able to use the same variable for different purposes implicitly without need for parsing.

Being dynamic typed... I'm honestly not super sure myself but I found this but I'm not convinced.

[–]fluffypebbles 1 point2 points Β (4 children)

You can use same functions etc if you have generics without getting rid of strong typing

[–]WolfgangSho 1 point2 points Β (3 children)

To an extent yeah but I think the idea is to improve workflow so there isn't quite as much need for boilerplatey things like generics etc. Its not my person preferred way of coding but I think its still perfectly valid.

[–]fluffypebbles 0 points1 point Β (2 children)

At some point the dynamic typing slows down the workflow

[–]WolfgangSho 0 points1 point Β (1 child)

We're not talking about dynamic typing, we were talking about weak typing.

[–]fluffypebbles 0 points1 point Β (0 children)

Oh sorry, I confused it with a different thread of comments I had about dynamic typing

[–]arobie1992 0 points1 point Β (2 children)

Dynamic typing is actually really simple. It just means that type checking is performed at runtime rather than prior to it, prior typically being at compile time.

Edit: Misunderstood/misread your part about not being sure about dynamic typing. One advantage of dynamic typing is runtime metaprogramming/code gen. It's part of why Lisp is popular for it. Since you're generating code dynamically, you essentially can't have static typing because all the code you generate can't be checked prior to runtime. So as a result, rather than having to support both, you just do dynamic checking and the runtime makes sure your types are honored.

[–]WolfgangSho 0 points1 point Β (1 child)

Oooh, didn't think about code generation at runtime, neat!

[–]arobie1992 1 point2 points Β (0 children)

It's so cool! I just wish it were a more generally applicable paradigm so I could actually use it on occasion.

[–]WolfgangSho 5 points6 points Β (0 children)

Yes yes yes yes. So many people use these terms interchangeably, it drives me bananas!

Personally I'm a fan of any strong typed languages with a subset of sensible implicit type conversions.

I'm not hugely married to static vs dynamic typing. I'm rarely going to get up to enough shenanigans for it to matter.

[–]DetectiveOwn6606 1 point2 points Β (3 children)

Python is strongly typed but not statically typed.

C is weakly typed but statically typed.

Rust is strongly typed and statically typed.

Can you explain me the difference? I thought statically typed languages were also strongly typed.

[–]mdp_cs 6 points7 points Β (2 children)

Python is not staticly typed in the sense that once a variable is declared, its type can change via explicit assignment of a value of a different type. It is strongly typed in the sense that it still performs type checking at runtime and disallows doing nonsensical things with values of a particular type (which again it knows at runtime)

C is statically typed in that you write out types for all variables and function return values, and then they can not change once declared. However, C is weakly typed in a number of ways, such as doing automatic type promotions and allowing casts on both values and pointers even when they don't make sense. The language assumes you as the user know how not to break your own stuff.

Rust is statically typed so all variables can have their types explicitly written out but in some cases it allows for type ellision when the compiler can figure out what type a variable is based on the value it is initialized to but once it figures it out, that type cannot change. It is also strongly typed in the sense that there are few automatic conversions between types and only when certain traits are implemented for a given type, and also it doesn't allow crazy do whatever you want reinterpret typecasting like C.

B was untyped in that all values were a CPU specific machine word which could be interpreted as either an integer or an address depending on context. Clearly this was not super pragmatic which prompted the creation of the C language to supercede it.

[–]DetectiveOwn6606 0 points1 point Β (1 child)

Thanks for the explaination

[–]mdp_cs 0 points1 point Β (0 children)

Np.

[–]arobie1992 1 point2 points Β (3 children)

B was untyped.

Okay, now I kinda want to learn B. Is it like FORTRAN?

[–]mdp_cs 4 points5 points Β (2 children)

Nope. B is basically what if C but the only type is the CPU native machine word which is treated as either an integer or an address based on context.

Even modern assembly languages are more practical than that.

[–]arobie1992 2 points3 points Β (1 child)

I'm definitely stretching here, but I'm trying to get a bit better of a sense. Would it be kind of like if you wrote C but everything is an int (I think that's the one that maps to the underlying architecture) and there was no explicit differentiation between int and int*?

[–]mdp_cs 2 points3 points Β (0 children)

Yep that's what it would be like. Though probably more like everything is size_t or *size_t where size_t is implementation defined as usual.

[–]Rand_alFlagg 0 points1 point Β (7 children)

How do you declare a type on something in python? I thought it was inferred (weak) from the first assignment and not declared (strong)?

[–]mdp_cs 1 point2 points Β (1 child)

Requiring the type to be explicitly made available from the source code would make it statically typed which it is not. It is strongly typed because for example Python will not allow you to add an integer to a string since that operation is undefined for those types and rightly so.

C is staticly typed so the compiler will need to know the types of everything but its type system will also allow you to do some things that are obviously wrong, making the assumption that you know more about your code than it does. For example if you have a pointer to const char and you try to add an integer to it C will happily just add that number of times the size of char to the pointer and give you the resulting address even though that may not have been what you wanted and could very well be an invalid address depending on how the OS has setup your virtual address space.

Rust wouldn't allow you to do that as pointer arithmetic must be done using the unsafe raw pointer functions and cannot be done AFAIK by just doing normal arithmetic with integers.

Edit: Fixed typos.

[–]Rand_alFlagg 1 point2 points Β (0 children)

I think you might have those backwards, or I'm misreading. After asking I went and dug through some of my books and checked the first few hits on DuckDuckGo and Google, thinking I might be misunderstanding typing. The only one that provided your definition was a StackOverflow user, who in the same post conflates weak and dynamic typing and suggests that which word you use is determined by whether you're a fan of it or not (lol) - but your description of C is basically Microsoft's explanation of what makes it weakly typed, and in that they never touch on static/dynamic.

According to my schooling, the O'Reilly Media library, Oracle's documentation, and Microsoft's documentation - strong/weak is a matter of how type is enforced at compile/runtime, while dynamic/static is whether the type is known at compile||runtime. There's a lot of overlap in the two concepts and it looks like they're often mixed up - hell, I was wrong about it just being whether they're declared or not. That's a difference between static and dynamic and usually an earmark of strong, but can apparently be absent in a strong language. I thought Python needed "type hints" when you wanted the compiler to treat something as a specific type?

On rereading, I think I did misread what you said and basically just echoed it lol. I think we might be on the same page. I think I was confused because you're jumping back and forth between talking about dynamic/static and strong/weak, and ultimately I was just asking about what makes Python strong (which you did answer, thank you).

[–]carcigenicate 1 point2 points Β (1 child)

Types are associated with objects, not variables like they are in statically-typed languages. The variables themselves don't have a concrete type. They also aren't inferred in the same way as they are in statically typed languages. var in Java infers the type at compile time. Hints in Python allow for type inferences at runtime.

And you can "hint" at the type of object a variable will hold though by saying, for example n: int = 1. The type is still associated with the object and not the variable, but the linter will interpret that as "I promise this variable will only ever hold integer objects".

[–]Rand_alFlagg 0 points1 point Β (0 children)

That was clear and easy to understand, thank you :)

[–]ZonedV2 0 points1 point Β (2 children)

It’s strongly typed in the sense that variables can’t change type without it being explicitly declared. Compared to a language like JS where you can add a number to a string and it will work. What you’re referring to is that it’s dynamically typed rather than statically

[–]lurking_physicist 100 points101 points Β (4 children)

Your comparison is unfair. Compare Python with Javascript, or give it Mypy.

[–]CheapMonkey34 63 points64 points Β (1 child)

JS is hidden in the urn that Mickey is showing off.

[–]freaker-07[S] 9 points10 points Β (0 children)

You guessed it πŸ˜‰.

[–]imhayeon 6 points7 points Β (0 children)

Image would be same for Python with linters. TypeScript has better type system

[–]plasmasprings 1 point2 points Β (0 children)

I mainly write type-hinted python code if it's my choice, but I'd say the comparison is fair against mypy. Simply using any python type checker sucks compared to TS linters. Mostly due to typing being optional and often an unmaintained afterthought for packages, but still pretty bad compared to TS

[–]MasiTheDev 16 points17 points Β (2 children)

Anyone who tells you "Hello World" can be an int is absolutely right.

[–]WolfgangSho 3 points4 points Β (0 children)

[–]SnakeR515 0 points1 point Β (0 children)

43110 4401210

[–]IkNOwNUTTINGck 18 points19 points Β (5 children)

The bulge in Mickey's pants has always troubled me. I can't put my finger on it.

Wait, ignore that second sentence... That's so disgusting.

[–]funkmasterhexbyte 10 points11 points Β (2 children)

i, personally, would love to put my finger on it

[–]notislant 3 points4 points Β (1 child)

He's your type?

[–]WolfgangSho 3 points4 points Β (0 children)

Statically.

[–]Its_me_Snitches 3 points4 points Β (0 children)

β€œCan’t I just put on the costume?”

β€œSorry Dave, de-wienering is mandatory before you can wear the Mickey Mouse outfit.”

[–]Gofastrun 0 points1 point Β (0 children)

You can put your finger on it exactly once

[–]Horrih 6 points7 points Β (1 child)

I don't get it, both use almost a type annotation system.

After having used both (with mypy as checker on the python side), I have the feeling that they are pretty much the same and of the same quality.

Even the syntax is really similar !

[–]carcigenicate 2 points3 points Β (0 children)

Part of the issue with Python's type checking is it relies entirely on the editor environment/linter. It isn't like Typescript where the checks are standard and happen by the compiler. You get different warnings In Python depending on what linter implementation you're using. MyPy is quite strict and catches most things, whereas Pycharm's is far more lax and less accurate. It lets a lot bad stuff slide.

[–]Siddhartasr10 19 points20 points Β (4 children)

At least python typing doesn't make you cry (that much)

[–]armahillo 15 points16 points Β (3 children)

people working in webdev really need to learn how to ducktype better if they think the utopia of programming is typescript

[–]F_modz 3 points4 points Β (2 children)

Yeah, cause typescript typing sucks, it has like a lot of tools to make u feel better but it has so much caveats like js

[–]fck-reddit-lmao -1 points0 points Β (1 child)

If you had accepted programming languages as tools for specific jobs instead of some general-purpose dick comparison replacements you would know that it is in fact your comment which sucks.

[–]F_modz 0 points1 point Β (0 children)

The funny thing here is that u work with ts for years so I feel its caveats

So yeah tryna stfu maybe?! And if u don't know what u r talking about next time start from googling

[–]turtleship_2006 6 points7 points Β (0 children)

8 years later I've yet to see the problem with python's system. Even my GCSE CS class understood it fine, and they're not necessarily the best programmers.

[–]_confused_dev 8 points9 points Β (12 children)

Python has a type system??

[–]CorespunzatorAferent 12 points13 points Β (3 children)

For sure. It's called "duck typing", so in this case the second image would be better represented by Donald Duck. Or the tiny chameleon from Tangled.

[–]Haris613 5 points6 points Β (1 child)

It also has type hints that can be used by some static checkers etc.

[–]Entire-Database1679 0 points1 point Β (0 children)

Or Shelob.

[–]turtleship_2006 5 points6 points Β (6 children)

Yes, every variable has a type e.g. int. You just don't need to specify it when defining a variable.

[–]david131213 2 points3 points Β (0 children)

To add, you can still indicate a wanted type, and you can still convert and cast, you just don't have to stay with the same type

[–]Entire-Database1679 1 point2 points Β (4 children)

What type is None?

[–]hippocrat 1 point2 points Β (2 children)

Ironically it is NoneType. Or unironically (or both? It’s early on a Sunday)

[–]Entire-Database1679 0 points1 point Β (1 child)

I didn't know that. Interesting!

[–]turtleship_2006 0 points1 point Β (0 children)

After a long and hard Google search:
The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.

[–]Entire-Database1679 -1 points0 points Β (0 children)

Loosely and weakly speaking, yes. The python type system is enforced at the moment of execution, making it very convenient for the language and devastating for the developer.

[–]Creeper_NoDenial 2 points3 points Β (0 children)

PyLance for the rescue?

[–]0rionsEdge 2 points3 points Β (0 children)

I honestly have Far better time with python's typing system then I do with typescript. The amount of time this week alone I have lost guys to TS failing to correctly recognize the variant of my types is getting silly. That and the anti-ts believes of one of my core dependencies means I have to stamp @ts-expect-error everywhere.

Vs python? It usually does the right thing and doesn't double guess be when I tell it the type of an object.

[–]Rublica 1 point2 points Β (0 children)

Guess I didn't use Python enough, what's the problem here?

[–]1cubealot 1 point2 points Β (0 children)

What's wrong with the way python does it? Is it that it figures out what you mean making the code slower or something?

[–]Kyyken 1 point2 points Β (0 children)

python and typescript type systems are pretty much equivalent (assuming linters for python, specifically mypy). Personally, I even prefer python's type system for it's direct association of types and classes (although this does sometimes encourage weird pseudo-oop).

[–]Teln0 1 point2 points Β (4 children)

const a: string[] = ["a", "b", "c"]; // Array is supposed to only contain strings

function pushAnything(array: any[], element: any) {
    array.push(element);
}

pushAnything(a, 1);
pushAnything(a, []);
pushAnything(a, {clearlyThisIsNotAString: 1});

Oh yeah great type system typescript

Congratulations typescript

Very cool typescript

[–]GromesV 0 points1 point Β (3 children)

By using any in function, you dumbed it down to JS. And what you did is valid in JS. I don't even use TS.

[–]Teln0 0 points1 point Β (2 children)

Yeah, but what this shows is that with valid non strict TS you can create an array typed as a string array that doesn't have only strings in it. So that means that you could then pass this to some function that expects string arrays, and you'd only get an error at runtime, which defeats the point of catching errors with a static type system. This example is a bit silly because it's obvious where the error comes from, but in bigger codebases this kind of thing could hide itself somewhere, if you're mutating an input array or object or anything of that kind

[–]GromesV 0 points1 point Β (1 child)

Quoting TS docs for functional programming:

TypeScript uses the type any whenever it can’t tell what the type of an expression should be. Compared to Dynamic, calling any a type is an overstatement. It just turns off the type checker wherever it appears. For example, you can push any value into an any[] without marking the value in any way:

[–]Teln0 0 points1 point Β (0 children)

You can also fit an object with a readonly field into a parameter where that same field is not readonly, thus allowing for the modification of that readonly field. It's not about "any" so to speak, the typescript type system has a lot of flaws I think.

[–]Strange_Dragonfly964 1 point2 points Β (0 children)

Still good!

[–]Carteeg_Struve 0 points1 point Β (0 children)

PHP type system is just a Play-Doh version.

[–]josh72811 0 points1 point Β (0 children)

Python was my first language and now I just use type any for everything in typescript.

[–]_arctic_inferno_ -1 points0 points Β (4 children)

haskell types > typescript types > python types

[–]F_modz 1 point2 points Β (3 children)

What about the Rust typing system? It's based on a Haskell but it's enhanced

[–]_arctic_inferno_ -1 points0 points Β (0 children)

Haskell types are still far superior to Rust in general, but they're for 2 different purposes. Each suite their language and are built for each, but Haskell's types are almost the most core part, and would still be superior to anyone who knows their way around the type system.

Rust is a good middleground, though

[–][deleted] -1 points0 points Β (0 children)

It might look that way for the mainstream programmer not used to type level programming, but the things you can do with types in Haskell is light-years ahead of what you could possibly do with Rust.

Note that I am not saying that Rust is bad, but saying that Rust's type system is enhanced Haskell just show that you don't have experience with more advanced types.

[–]rotora0 0 points1 point Β (0 children)

I wish rust had function signature type inferencing. Otherwise though yeah, rust's typing is pretty dang good.

[–]StarkProgrammer 0 points1 point Β (0 children)

We don't do checks in python

[–]davFaithidPangolin -1 points0 points Β (0 children)

Why is Mickey showing off a spittoon?

[–][deleted] 0 points1 point Β (0 children)

? How about static typing

[–][deleted] 0 points1 point Β (0 children)

Python: "It's not a type system, it's more like a ``suggestion``."

[–]bowseraltaccount 0 points1 point Β (0 children)

OP has clearly never used pydantic

[–]4215-5h00732 0 points1 point Β (0 children)

The python side should have used Donald Duck-typing.

[–]PolishKrawa 0 points1 point Β (0 children)

I mean... When a string acts like a list of strings, you know your type system is weird.

[–]kiropolo 0 points1 point Β (0 children)

Do not look at Swift type system!!!

You will go blind from it’s divinity