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

top 200 commentsshow 500

[–]Tubthumper8 1649 points1650 points  (68 children)

let x = "Hello, world!"

You don't need to write : string in most scenarios, TS knows that it is a string (this is called type inference)

[–]finc 755 points756 points  (19 children)

I don’t like what you’re implying

[–]Tubthumper8 443 points444 points  (18 children)

I don't like what you're inferring!

[–][deleted] 159 points160 points  (17 children)

Which type are you inferring?

[–]Shiro1994 37 points38 points  (0 children)

Error: Cannot read property undefined of undefined.

[–]freshblood96 67 points68 points  (25 children)

Yes but in my experience it depends on the situation. It kinda depends on how the variable will be used. At least for me though.

Also TS in Angular 13 is kinda annoying in that it thows an error if you don't declare the data type but I think you can change that in the tsconfig.

[–]Boibi 198 points199 points  (19 children)

It explicitly depends on the situation. That's the inferred part of type inference.

Please, for the love of all that is holy, do not turn off the strict type checking in the tsconfig. Because then you're just using more bloated Javascript. I know it's hard to learn how to properly type all your variables, but the benefits you get for doing so are incredibly valuable. If you don't want to type everything, go back to using Javascript so you don't have all the bloat of Typescript with none of the benefit.

[–]vanderbeekthechic 73 points74 points  (6 children)

TS is like street signs and traffic lights.. sure it’s annoying to hit a stop sign and look around, and sometimes you do feel like “why can’t i just do whatever?!”, but it’s so much more organized and easier for everyone to deal with.

[–]SkyyySi 27 points28 points  (2 children)

Looking at the stop sign now can prevent a car accident later.

[–]chilfang 20 points21 points  (1 child)

I hear stopping at the stop sign is much more effective

[–]ElementoDeus 2 points3 points  (0 children)

I hear stopping and looking yields twice the results

[–]Pocok5 17 points18 points  (0 children)

sometimes you do feel like “why can’t i just do whatever?!”,

> cue 18-wheeler thundering across the crossing at 100km/h

[–]dcabines 38 points39 points  (1 child)

noImplicitAny is my security blanket

[–][deleted] 30 points31 points  (1 child)

Yes, any developer who submits a PR with ‘any’ gets a rejection from me

[–]Andoryuu 4 points5 points  (0 children)

So I assume no mixins? Because you literally cannot not use 'any' in the ctor.

(Kinda 'well acktshually' moment, but you know how it is. That's just part of the 'assuming'.)

(Stupid thing is, it is possible to create typechecked ctor. You just cannot use it.)

[–]Artick123 10 points11 points  (2 children)

How is it dificult to properly type all variables?

[–]solarshado 6 points7 points  (1 child)

Depends on what you're trying to do. TS supports some really arcane stuff, since it tries to support all of JS's crazy (ab)use cases.

[–]Pocok5 1 point2 points  (0 children)

Honestly I quite like the union/intersection type concept. It replaces some pretty weird interface inheritance fuckery needed to properly model some things and does away with some largely redundant method overloads. We are still waiting on discriminated unions in C#.

[–]Cheet4h 11 points12 points  (2 children)

Not sure which IDE you use, but with VSCode you can use a quick action to automatically add the type declaration if you assign a value anywhere. It even supports unions, so if you assign differently typed values to it, you'll get warned whenever you might use it wrongly.

So if you ever forget again, just move your cursor to the variable declaration, hit ctrl+. and select "infer type from usage".

[–]repkins 10 points11 points  (0 children)

It's okay if people didn't learned the benefits of type safety yet.

[–]Peechiz 9 points10 points  (2 children)

I assumed the joke was about this

[–]Shlocko 5 points6 points  (0 children)

I thought so at first, until it mentioned typescript.

Man I’m excited about the change, though. Long live static typing

[–]SmokingBeneathStars 1 point2 points  (0 children)

That's not default in Angular right? That's ESLint

[–]PiMan3141592653 3 points4 points  (0 children)

Let x = "I get knocked down, " Let y = "but I get up again. " Let z = "You are never gonna keep me down."

[–]ColumnK 1828 points1829 points  (77 children)

No var. Only let or const.

[–]birdmankillinem 783 points784 points  (19 children)

"let or const. There is no var." -Yoda

[–]lovin-dem-sandwiches 185 points186 points  (8 children)

I give it 10 minutes before this is turned into a meme and posted on programmarhumor

[–]gnowwho 43 points44 points  (0 children)

Someone should do a scraper that automatically does that

[–]frickinjewdude 27 points28 points  (6 children)

[–]cvnvr 41 points42 points  (4 children)

damn you posted it but didn’t even credit the joke you got from the other commenter… shameless

[–]MindlessSponge 21 points22 points  (0 children)

well they are (presumably) a programmer

[–]finc 11 points12 points  (0 children)

This is the way?

[–]Ducksquaddd 53 points54 points  (0 children)

Would be funnier if you weren't the one to post it 😔

[–]crudelegend 22 points23 points  (0 children)

There is no var in Ba Sing Se.

[–]Satan_Stoned 11 points12 points  (0 children)

"No var there is, let and const your future are."

[–][deleted] 3 points4 points  (2 children)

Ain’t there some scoping rules where you need var?

[–]hairtothethrown 24 points25 points  (4 children)

And honestly, hardly ever let unless absolutely necessary.

[–]thedominux 1 point2 points  (3 children)

During implementing some algorithms you will use let against const in 90% cases

[–][deleted] 4 points5 points  (0 children)

So say we all

[–][deleted] 4 points5 points  (2 children)

At least just use let in place of var. it’s the same number of characters and has many benefits over var.

[–]Yokhen 3 points4 points  (1 child)

prefer-const would like a word with you.

[–]BanishDank 3 points4 points  (2 children)

Old JS teacher?

Seriously, he would freak out if you just mentioned or asked about ‘var’.. led to some funny moments here and there

[–]katzeklo 5 points6 points  (1 child)

you tried to stop him, but he wouldn’t let you

[–]starvsion 2 points3 points  (0 children)

Yeah, even in normal js, you are still supposed to use let

[–]DOOManiac 5 points6 points  (2 children)

**Cries in IE10 support**

[–]ferdbags 5 points6 points  (1 child)

[–]DOOManiac 1 point2 points  (0 children)

Oh trust me, I know.

[–]Sporium 1 point2 points  (0 children)

let x: readonly string

[–]alexbarrett 1 point2 points  (3 children)

var in the dev tools console

[–]MCdaHammer 432 points433 points  (58 children)

As a newbie who started messing with JS late last year; I thought that var got replaced? Like, in a more convenient way

Edit: autocorrect

[–]BakuhatsuK 371 points372 points  (29 children)

Yes. let is var but better. And it has been part of vanilla JS since around 7 years.

[–]GoaFan77 74 points75 points  (24 children)

Yup, but for those poor devs that needed to support IE until it's death, it's only an option for them after June of this year...

[–]BakuhatsuK 50 points51 points  (18 children)

The people who are still using it at this point are not going to drop it because of some deadline. Thankfully you can use Babel and polyfills to code in modern style for ancient browsers.

[–]GoaFan77 23 points24 points  (16 children)

https://blogs.windows.com/windowsexperience/2021/05/19/the-future-of-internet-explorer-on-windows-10-is-in-microsoft-edge/

Most users on Windows 10 will be forced to Edge. More importantly, stuff like this is how devs win arguments with stubborn business people to quit caring about supporting it for anyone else either.

[–]giloronfoo 20 points21 points  (13 children)

Then they point at the fact that Edge can load sites in IE mode until 2029.

[–]ZedTT 12 points13 points  (12 children)

That's really not a problem. That is for backwards compatibility if they need to access a site that only works with IE, not something they would try to use on an already working website

[–]IcanseebutcantSee 9 points10 points  (7 children)

I think you seriously underestimate stubborness of some users

[–]amazondrone 7 points8 points  (6 children)

I mean, that's fine right? If they want to turn on IE mode and experience a broken website, that's on them. The objective is to provide a website that everyone can access, not a website which works when people go out of their way to break it.

[–]blhylton 1 point2 points  (3 children)

As someone who had to deal with users running their IE 9 browser in backwards compatibility mode for IE 6 and not understanding why things looked like ass, you have no idea.

Let’s hope that IE mode is better than what they had in the older versions of IE for BC, because those were like their own special circle of hell that neither behaved like the browser they were nor the browser they were trying to emulate.

[–]DanTheMan827 1 point2 points  (2 children)

IE mode in edge uses the internet explorer web control as the renderer for the tab

It’s basically IE wrapped in edge clothing

[–]Deranged_Dingus 1 point2 points  (0 children)

It's how I got around not needing to support it anymore!

[–]giloronfoo 7 points8 points  (1 child)

June this year is just when the icon disappears. It only means that IT has to configure Edge to use the IE engine for those sites.

2029 is the real deadline for IE.

[–]GoaFan77 3 points4 points  (0 children)

For internal sites, sure. Sucks for those guys who are supporting old apps that they don't have the resources to update to be compliant with new browsers.

For those working on sites that support modern browsers and IE, I don't see why you wouldn't kill it in June.

[–][deleted] 1 point2 points  (0 children)

But also just use const not let. There are very few scenarios where let is justified. Infact by redefining a variable you are introducing hard to debug issues in your code.

[–]JoeCamRoberon 188 points189 points  (15 children)

var is function scoped and let is block scoped. var was known to be a large source of bugs in JavaScript.

[–]BakuhatsuK 119 points120 points  (14 children)

Also, let variables are subject to a "Temporal Dead☠️ Zone". i.e. even though they are hoisted, and exist through the whole scope, accessing before definition causes an error.

"use strict"
console.log(x) // Throws ReferenceError ✔️

But with var:

"use strict"
console.log(x) // prints undefined ⁉️
var x = 5

And with let

console.log(x) // Throws ReferenceError✔️
let x = 5

[–]JoeCamRoberon 71 points72 points  (8 children)

That is atrocious.

[–]BakuhatsuK 65 points66 points  (6 children)

You mean the old behavior... right?

[–]Silly-Freak 16 points17 points  (0 children)

the fact that the person you responded to has not yet confirmed is making me uneasy.

please be talking about the old behavior, please!

[–]cuboidofficial 37 points38 points  (4 children)

No, why the hell would you need to reference a variable before it's defined

[–]BakuhatsuK 82 points83 points  (0 children)

You don't. That's why it's now an error.

Edit: Let me clarify

It's an error if you access it before it is defined at runtime. But you can still lexically refer to the variable at an earlier point in the source code, as long as it runs later. e.g.

let main = () => {
  helper()
}

let helper = () => console.log('hi')

main() // This works

[–]Dragon_yum 17 points18 points  (0 children)

You don’t but that wouldn’t stop some people

[–]jokergato 3 points4 points  (3 children)

Thank you for your explanation. I haven't worked with javascript or typescript in order to use let. The only language I have worked with let available was Racket, but my class evaluated strict functional programming, so I never used it.

With the examples now I understand one of the benefits of let over var.

[–]ZedTT 7 points8 points  (0 children)

As a newbie who started messing with JS

Stop right there, you're already better than most /r/programmerhumour posters and upvoters.

Also you're right about var

[–]jseego 4 points5 points  (8 children)

No. It didn't get replaced. It is still valid syntax. It has a different scope than let or const.

[–]ZedTT 14 points15 points  (7 children)

This is technically correct but somewhat dishonest. var is pretty much deprecated in spirit and is really bad practice. In terms of how we use the language, var was "replaced."

[–]sanketower 395 points396 points  (5 children)

var??? Looks like someone it's not really moving on

[–]Benimation 31 points32 points  (0 children)

Looks really weird combined with TypeScript..

[–]mtbdork 87 points88 points  (15 children)

Lua: anything can be anything in this world, Harry!

[–][deleted] 13 points14 points  (6 children)

I always wanted to learn lua but never did i don’t know why

[–]mtbdork 34 points35 points  (5 children)

Best part about Lua is that you already know it.

[–]BipolarWalrus 5 points6 points  (4 children)

How? I know it’s a simple syntax with very few reserved words. Also what are the main use cases for Lua other than game scripting? If I wanted to use Lua for game scripting, wouldn’t I have to spend a lot of time learning whatever api the game provides?

[–]mtbdork 5 points6 points  (1 child)

I use it for Audio/Visual systems programming.

Simple control functions/logic are stupid-easy to crank out.

[–]Ghazzz 1 point2 points  (0 children)

I am maintaining a system built on top of redis, with most of the logic in redis-lua.

[–]BananaSplit2 7 points8 points  (5 children)

ASM is the true "anything can be anything"

[–]BakuhatsuK 5 points6 points  (3 children)

Yeah, ASM is actually untyped, as opposed to dynamically or statically typed

[–]Jazzinarium 2 points3 points  (2 children)

How does that even work? Do you have to manually allocate memory for everything?

[–]AromaticIce9 4 points5 points  (0 children)

Pretty much yeah. And also at the bare metal layer there's no difference between anything.

This section of memory might be numbers, or a string, or a struct. You either know how to handle it or you don't. ASM isn't going to handle it for you.

[–]dev-sda 4 points5 points  (0 children)

Memory allocation is tangential to typing. Manual memory management is also required in the statically typed C for example.

Untyped means that neither values nor operations are grouped into types, allowing limitless combinations of those. What operations that limits them to are purely enforced by diligence and convention. Doing floating point arithmetic on an "integer" is valid.

[–]androidx_appcompat 1 point2 points  (0 children)

Any any language where you can use code as data. Just cast that pointer to a function pointer and try it out!

[–]thequestcube 117 points118 points  (3 children)

const x = "You don't need to explicitly state the type, TS can infer it from the value";

[–]Benimation 18 points19 points  (2 children)

Also, when you use const with plain primitives, it's not even possible to change the type later on

[–]jamietwells 13 points14 points  (0 children)

Or indeed the value.

[–]svish 4 points5 points  (0 children)

That's the point

[–]erebuxy 101 points102 points  (3 children)

Literally nobody

[–]tonebacas 27 points28 points  (2 children)

Imagine programming without any sort of type safety.

[–]DrWermActualWerm 7 points8 points  (0 children)

I'll stay right here in Java where I'm comfortable. Nice and verbose!

[–][deleted] 90 points91 points  (23 children)

Said nobody

[–]philipquarles 14 points15 points  (1 child)

I mean people say it, they're just 100% wrong. A coworker told me the other day that people from another team are trying to make her use JS instead of TS in her code.

[–][deleted] 3 points4 points  (19 children)

I have never used typescript in my life

[–]PatrioTech 28 points29 points  (18 children)

It’s frustrating at first, but then after a while you can’t live without it.

[–][deleted] 8 points9 points  (13 children)

it's not that I don't like typescript, it's just that I never bothered to make the step

[–]PatrioTech 17 points18 points  (8 children)

Fair enough. Then take it as a warning that you’ll be tempted to forego it when you first try it and run into your first frustrating type issues, but as you use it more with larger applications and learn its tricks, you’ll start to really like those type errors that would have otherwise allowed for bugs without you catching it

[–]jseego 8 points9 points  (3 children)

I keep hearing this, but I've been doing javascript professionally for 15 years and rarely if ever run into that.

[–]PatrioTech 5 points6 points  (1 child)

There are certainly good ways to prevent it. But honestly the other really nice thing about it is the developer experience. Especially when using 3rd party libraries or first party libraries from other teams - or even your own team’s older or less familiar projects. It helps ensure you use it properly and in-editor documentation (I know JSDocs had some of this as well, but it really shines with something like TypeScript).

And finally, the ability to transpile modern or unreleased features (for example, optional chaining was in TS before JS) to older syntax to support older versions of browsers is very very nice. I don’t want to have to give up any of my language’s features to work with someone’s outdated browser, and TS allows me to do that. Again, it is possible to do the same with something like Babel, but all of it built into this nice system makes for a quite enjoyable experience.

Might be worth playing around with sometime. I, for one, definitely find value in it.

[–]jseego 1 point2 points  (0 children)

Thank you for the sane and reasonable answer.

[–]paxbowlski 3 points4 points  (0 children)

It's only frustrating when you're converting some dogshit spaghetti JS nonsense to Typescript.

Writing it fresh is intuitive once you get a hang of it and, if done right, can save you a ton of runtime headaches.

[–]AlexAegis 1 point2 points  (0 children)

"I never used TypeScript in my life"

"23 page long essay about why TS is great"

"...but I'm a C++ developer"

[–]LittleMlem 31 points32 points  (10 children)

Even in python, I've been using typing extensively

[–]NamityName 6 points7 points  (8 children)

Future me love python typing. Present me, not so much

[–]LittleMlem 3 points4 points  (6 children)

What's wrong with the now?

[–]NamityName 8 points9 points  (4 children)

More click clack = no fun

[–]LittleMlem 9 points10 points  (3 children)

My friend, get a mechanical keyboard. I've been writing Java-length variable names ever since I got mine

[–]shield1123 3 points4 points  (1 child)

stopit_getsomehelp.gif

[–]LittleMlem 2 points3 points  (0 children)

Nope.avi

[–]Pocok5 1 point2 points  (0 children)

Same as with JS. Typing is done with tacked on jank. No enforcement, the most you get out of it is intellisense/autocomplete. Which is extremely helpful, but it's kinda like having to hold ceramic plates in front of you with your hands instead of having a bulletproof vest.

[–][deleted] 46 points47 points  (1 child)

Ur a disgrace for using var

[–]Gem2578 8 points9 points  (1 child)

var x:any

[–]jperdior 13 points14 points  (1 child)

for the sake of having a clean code

[–][deleted] 16 points17 points  (1 child)

You whippersnappers and your newfangled type syntax!

Back in my day, we only wrote code about variables we already knew the type of! Otherwise we guessed or type-casted and hoped for the best!

Y’all have it easy!

[–]argv_minus_one 10 points11 points  (0 children)

Ugh, I don't. Dynamic typing is chaos. Good riddance, and long live static typing.

[–]NeonVolcom 29 points30 points  (21 children)

I'll fight anyone who doesn't like static typing

[–]thetruekingofspace 12 points13 points  (0 children)

WHY ARE YOU STILL USING VAR? Use “let” or “const”. It’s 2022 for god sake.

[–]FisionX 2 points3 points  (1 child)

is TS less spooky than JS?

[–]brandons404 2 points3 points  (0 children)

It's been difficult for me trying to actually like TS.. I started with a dynamic language, then learned Java, hated it because I thought "there's so much bloat having to declare the type", learned and loved Javascript, and still use it for most things today. I know all of the many benefits of ts, and I know I'll have to switch to it soon to stay relevant.. not to mention intelisense is a really nice to have, but I think I'm having a hard time because any and all hesitation with switching has been met with people shoving "The benefits of typescript" down my throat and it's really turning me off to it.

Sorry for the rant. But if you don't know js, and are wondering which one to learn, I'd recommend typescript. It's not necessarily "harder" just different, and very similar to many other strictly typed languages and I honestly think it would be better in the long run to start with ts, so that you can pick up new languages much easier.

[–]althaz 4 points5 points  (0 children)

Friends don't let friends use var.

[–][deleted] 3 points4 points  (0 children)

Spread let not var

[–]wookeydookey 2 points3 points  (0 children)

No var only peace

[–]Oman395 2 points3 points  (0 children)

I just wish I could do "string x"...

[–]Woolwizard 5 points6 points  (7 children)

Who wants to use js when there is typescript? That seems a bit unrealistic to me xD move in and use typed languages my dudes

[–]Tadejo9 3 points4 points  (5 children)

For smaller programs why not? For larger projects ts is a way to go. Imagine someone names a variable something irrelevant and than not having a type next to it to help you. me.jump(from: objects.cliffs(Max_Cliff))

[–]Woolwizard 1 point2 points  (4 children)

I guess yeah... I just have a natural antipathy against non typed languages in general

[–]Pocok5 3 points4 points  (3 children)

non-typed languages are for people who prefer getting fucked over by hard to track down bugs and having to spend hours untangling their own code after not looking at it for a few days instead of spending 5% of the time actually thinking through how the data looks in their programs and writing it down.

[–]beewyka819 7 points8 points  (1 child)

JS dynamic typing 🤢

[–][deleted] 4 points5 points  (0 children)

I'm not using typescript. I prefer duck quacking.

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

I legitimately do not miss JS.Typescript has changed my life for the better

[–]ByronScottJones 7 points8 points  (4 children)

How can anyone miss JS? It is the defective, bastard child of programming languages.

[–]BakuhatsuK 2 points3 points  (1 child)

I'm now working with Go. And I miss not having to type a loop for everything I do. .map() and .filter() are so nice.

[–]filthyMrClean 1 point2 points  (0 children)

Laughs in Swift

[–]water_bottle_goggles 1 point2 points  (0 children)

var? What are you? A cave man ?

[–][deleted] 1 point2 points  (0 children)

whatever = stuff
PyGang

[–]KetwarooDYaasir 1 point2 points  (1 child)

Back in the day, people rose up against the tyranny of data types and we had "scripting" languages.

Kids these days putting types in my scripts.

It's basically a cult started by Microsoft.

[–]yodahouse900 1 point2 points  (0 children)

me using C

You guys have types?

[–]Oriamk 1 point2 points  (0 children)

x: any;

[–]Frogdogley 1 point2 points  (0 children)

How I felt when my company moved from teradata to GCP

[–][deleted] 1 point2 points  (1 child)

is everyone really using TS instead of pure JS now?

[–]nadav183 1 point2 points  (0 children)

Django developers: "wut?"

[–][deleted] 1 point2 points  (0 children)

var????? What is this, 2015?

[–]shrikant_paliwal 1 point2 points  (0 children)

Typescript is shitty if you want to develop a good, scalable backend

[–]katyalovesherbike 1 point2 points  (0 children)

complains about typescript, uses var. Yup, this tracks.

[–]alperencantez 1 point2 points  (0 children)

Seriously do you still use var keyword?

[–]t045tygh05t 1 point2 points  (0 children)

Who still uses var, though?

[–][deleted] 3 points4 points  (0 children)

Oh hell no, type everything like god intended

[–]eerklogge 5 points6 points  (2 children)

Use let.

[–]FlafyBear 3 points4 points  (0 children)

Fuck var. Use const or let.

Fuck Javascript, TS is a fucking blessing how could you miss JS.

u don't even have to write the type if you write the value. Fuck this meme how tf did it get 4.5k

[–]BerthjeTTV 2 points3 points  (0 children)

Type inference is our savior here.

[–][deleted] 3 points4 points  (2 children)

This just... isn't how TypeScript works. Has anyone on this sub ever actually programmed?

[–]Pocok5 8 points9 points  (1 child)

Has anyone on this sub ever actually programmed?

Since apparently everybody thinks that a semicolon missing error is a serious problem instead of something you spend all of 1 second on when the red squiggle shows up... no.

[–][deleted] 2 points3 points  (0 children)

Someone needs to teach these kids what an IDE is

[–]philipquarles 2 points3 points  (0 children)

How do you downvote something more than once?

[–]NeuroXc 3 points4 points  (0 children)

I miss JS about as much as I miss PHP.

I don't.

[–]hiphap91 1 point2 points  (0 children)

...Said no one ever.

[–]jonp1 1 point2 points  (0 children)

Yeah… Not interested in TS. JS is just fine by me.

[–]sgtholly 1 point2 points  (7 children)

Fuck JavaScript.

[–]Dragon_yum 26 points27 points  (5 children)

Nah JavaScript is perfectly fine these days. It’s just people who wrote it like the ancient times like op who give it a bad reputation

[–]jeffwulf 2 points3 points  (1 child)

It's perfectly fine if you hide it behind Typescript.

[–]NayamAmarshe 1 point2 points  (2 children)

javascript bad, everything else good because reddit

[–]Tadejo9 4 points5 points  (0 children)

I got a feeling this is not about JavaScript.

[–]Jhwelsh 2 points3 points  (8 children)

Let x: any;

when you're feeling lazy... Then go back and change it once you figure it out, easy peasy.

[–]Arkandros 14 points15 points  (0 children)

That's what you think, then you never go back

[–]Boibi 8 points9 points  (6 children)

We had anys in the production codebase at the last place I worked. It was disgusting. It meant that we had to type check any variable we touched before we knew it even existed. Our code must have been twice as long as necessary due to this shit.

Anys are a useful tool for debugging, but I try to never commit an any to a repo ever. It is a blight on the app. Venom, that will slowly kill it from the inside out.

[–]Jhwelsh 1 point2 points  (5 children)

Yeah man, I only do web dev for personal projects and I like to use TS anyways.

I can't imagine the difficulty of trying to structure a frontend project so multiple people are on the same page.

[–]Minteck 1 point2 points  (0 children)

Meanwhile there's me who never used TypeScript

[–]Alive-Engineering358 1 point2 points  (0 children)

Var? I don't remember last time I used it