Live data from Hacker News

TypeScript 3.7

typescriptlang.org

41–50 of 167 posts

Re: TypeScript 3.7

#41

The optional chaining and null coalescing operators are very nice. Dart has had those for several years and they really do come in handy.

Definitely. It's a shame that dart gets so much unwarranted hate. I mean, I also think it's an absolutely awful language but it's really proven to be a valuable source of data for what other programming languages should do and perhaps more importantly: not do. I really hope we see a lot more things like Dart, and not so much negativity.

Re: TypeScript 3.7

#42
post #29

For this code: const x = [1,2]; const y = x[666]; const z = y + 3; Is there a way for TypeScript to flag the last line as a type error? TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?

Perhaps explicitly setting the types might do it? const x: number[]= [1,2] const y: number | undefined = x[666] const z: number = y + 3 EDIT: Just tried, still works

It works because `undefined + number -> NaN`, and NaN is a number.

Re: TypeScript 3.7

#43
post #38

Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code? log?.(`Request started at ${new Date().toISOString()}`); // roughly equivalent to // if (log != null) { // log(`Request started at ${new Date().toISOString()}`); // }

[deleted]

Re: TypeScript 3.7

#44

I feel really excited about 3.7. Optional chaining and null coalescing will clean up a TON of code. ... but with that being said, 3.7 seems to have broken many aspects of the `Promise.all` interface. Right now the largest issue seems to be that if any `Promise` result in `Promise.all` is nullable, all of the results are nullable.

Indeed, how can you declare a list with some elements nullable, and some not?

The result should instead be a tuple, but IDK how well tuple size inference would work in a case like that.

Re: TypeScript 3.7

#45
post #38

Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code? log?.(`Request started at ${new Date().toISOString()}`); // roughly equivalent to // if (log != null) { // log(`Request started at ${new Date().toISOString()}`); // }

The latter. You can look at what javascript is emitted on the playground.

  let bar: any, log: any;
  log?.(`foo ${bar()}`);
  
  // becomes
  var _a;
  var bar, log;
  (_a = log) === null || _a === void 0 ? void 0 : _a("foo " + bar());

Re: TypeScript 3.7

#46

Earlier quoted context omitted.

https://www.typescriptlang.org/play/index.html#code/MYewdgzg... Using `as const` will report both the 2nd and 3rd lines as type errors. You've been able to do this in TypeScript for a while even before they introduced the `as const` syntax.

Thanks, you can still trick it with this though (there's no type error): const x = [1, 2] as const; const r = 666 + 1; const y = x[r]; const z = y + 3;

Let's wait until TS supports dependent types!

Re: TypeScript 3.7

#47
post #38

Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code? log?.(`Request started at ${new Date().toISOString()}`); // roughly equivalent to // if (log != null) { // log(`Request started at ${new Date().toISOString()}`); // }

Why would it not match up with the example given? They provided it so you'd know the answer to your question.

Re: TypeScript 3.7

#48
post #26

Earlier quoted context omitted.

Not really https://github.com/microsoft/TypeScript/issues/9235 Though with tuples, etc. being defined, maybe it's worth re-examining.

Thanks. Is there a pattern for array access that helps with my example? Is the only option to define your own safe access function like "function safeGetFromArray (array: T, index:number): T | undefined"? I haven't tried it yet but it looks like the new optional element access feature is only checking if the array itself is defined, not if the array index is defined.

I can't seem the find the GitHub issue for it off hand, but I believe you can override the default indexing signature for arrays to return possibly undefined.

Otherwise, you're left to creating a wrapper function and a custom ESlint rule.

Edit: found it https://github.com/microsoft/TypeScript/issues/13778#issueco...

The suggestion is to maintain your own copy of `index.d.ts` with the array indexing interface modified. Yikes!

Re: TypeScript 3.7

#49
post #38

Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code? log?.(`Request started at ${new Date().toISOString()}`); // roughly equivalent to // if (log != null) { // log(`Request started at ${new Date().toISOString()}`); // }

`?` will short-circuit. Nothing is evaluated after that.

Re: TypeScript 3.7

#50
post #13

I really like the new optional operator, this might be what gets me to bite the bullet and start moving some of my projects over to typescript - dealing with potential undefined objects in those chains is one of the things I actively dislike about writing in vanilla Javascript.

I'm assuming by optional operator you're referring to optional chaining? If so, it's very cool but strikes me as an odd reason to move to TypeScript, because it's a stage 3 proposal in JavaScript too, so is likely to be widely supported soon.

This is correct. Move to TypeScript for the types. If all you want is the latest syntactic developments from ES, babel is a better fit. It tends to be further ahead than TS for the bleeding edge features.
Post reply on HN