News for hackers. Stuff that matters.
Re:Node.js adds experimental support for TypeScript by pphysch on Thursday July 25, 2024 @12:38AM (#41064760)
> In Python, I've even heard of people writing types in source code but never checking them

This is my main approach. Type hints are wonderful for keeping code legible/sane without going into full static type enforcement which can become cumbersome for rapid development.

Re:Node.js adds experimental support for TypeScript by josephg on Thursday July 25, 2024 @12:57AM (#41064851)
You can configure typescript to make typing optional. With that option set, you can literally rename .js files to .ts and everything "compiles" and just works. Adding this feature to nodejs means you don't even have to set up tsc if you don't want to.

But if I were putting in type hints like this, I'd still definitely want them to be statically checked. Its better to have no types at all than wrong types.

Re:Node.js adds experimental support for TypeScript by pansa2 on Thursday July 25, 2024 @01:33AM (#41065034)
> Its better to have no types at all than wrong types.

I agree - but the type systems of both Python and TypeScript are unsound, so all type hints can potentially be wrong. That's one reason why I still mostly use untyped Python - I don't think it's worth the effort of writing type annotations if they're just going to sit there and tell lies.

Or maybe the unsoundness is just a theoretical issue - are incorrect type hints much of a problem in practice?

Re:Node.js adds experimental support for TypeScript by zoul on Thursday July 25, 2024 @01:49AM (#41065106)
In my experience unsoundness is almost never a problem in practice, see here for details:

https://effectivetypescript.com/2021/05/06/unsoundness/

Re:Node.js adds experimental support for TypeScript by hajile on Thursday July 25, 2024 @03:35PM (#41072495)
Unsound types are never a problem until they are at which point you are staring at code that SHOULD be working, but is somehow breaking.
Re:Node.js adds experimental support for TypeScript by lolinder on Thursday July 25, 2024 @01:49AM (#41065108)
I've been using TypeScript professionally for 6+ years and have only ever run into issues at the border between TypeScript and other systems (usually network, sometimes libraries that don't come with types). There are a few edge cases that I'm aware of, but they don't really come up in practice.
Re:Node.js adds experimental support for TypeScript by kcrwfrd_ on Thursday July 25, 2024 @02:05AM (#41065199)
Is this “unsound”-ness that you’re referring to because it uses structural typing and not nominal typing?

Fwiw I’ve been working with TypeScript for 8+ years now and I’m pretty sure wrong type hints has never been a problem. TS is a God-send for working with a codebase.

Re:Node.js adds experimental support for TypeScript by nyssos on Thursday July 25, 2024 @09:35AM (#41068522)
There's not much connection. Typescript's record types aren't sound, but that's far from its only source of unsoundness, and sound structural typing is perfectly possible.
Re:Node.js adds experimental support for TypeScript by lolinder on Thursday July 25, 2024 @09:44AM (#41068617)
Soundness is also a highly theoretical issue that I've never once heard a professional TypeScript developer express concern about and have never once heard a single anecdote of it being an issue in real-world code that wasn't specifically designed to show the unsoundness. It usually only comes up among PL people (who I count myself among) who are extremely into the theory but not regularly coding in the language.

Do you have an anecdote (just one!) of a case where TypeScript's lack of type system soundness bit you on a real application? Or an anecdote you can link to from someone else?

Re:Node.js adds experimental support for TypeScript by kcrwfrd_ on Thursday July 25, 2024 @12:14PM (#41070379)
Ah someone else posted a link and I understand the unsoundness now.

The only time an issue ever came up for me was in dealing with arrays

  let foo: number[] = [0, 1, 2]

  // typed as number but it’s really undefined
  let bar = foo[3]
But once you’re aware of the caveat it’s something you can deal with, and it certainly doesn’t negate the many massive benefits that TS confers over vanilla JS.
Re:Node.js adds experimental support for TypeScript by lolinder on Thursday July 25, 2024 @12:46PM (#41070718)
Yeah, that example is unsound in the same way that Java's type system is unsound, it's a compromise nearly all languages make to avoid forcing you to add checks when you know what you're doing. That's not the kind of problem that people usually are referring to when they single out TypeScript.
Re:Node.js adds experimental support for TypeScript by AfterAnimator on Tuesday July 30, 2024 @09:25AM (#41108951)
For this case, I've switched to using `foo.at(3)` now instead, as it returns `T | undefined`, so you have to handle the undefined case.
Re:Node.js adds experimental support for TypeScript by nyssos on Thursday July 25, 2024 @01:36PM (#41071247)
> Do you have an anecdote (just one!) of a case where TypeScript's lack of type system soundness bit you on a real application?

Sure. The usual Java-style variance nonsense is probably the most common source, but I see you're not bothered by that, so the next worst thing is likely object spreading. Here's an anonymized version of something that cropped up in code review earlier this week:

    const incomingValue: { name: string, updatedAt: number } = { name: "foo", updatedAt: 0 }

    const intermediateValueWithPoorlyChosenSignature: { name: string } = incomingValue

    const outgoingValue: { name: string, updatedAt: string } = { updatedAt: new Date().toISOString() , ...intermediateValueWithPoorlyChosenSignature }
Re:Node.js adds experimental support for TypeScript by lolinder on Thursday July 25, 2024 @03:40PM (#41072556)
I mean... yes, there's a footgun there where you have to know to spread first and then add the new properties. That's just a good practice in the general case: an intermediate type that fully described the data wouldn't have saved you from overwriting it unless you actually looked closely at the type signature.

And yes, TypeScript types are "at least these properties" and not "exactly these properties". That is by design and is frankly one reason why I like TypeScript over Java/C#/Kotlin.

I'd be very interested to know what you'd do to change the type system here to catch this. Are you proposing that types be exact bounds rather than lower bounds on what an object contains?

Re:Node.js adds experimental support for TypeScript by haskman on Friday July 26, 2024 @04:58AM (#41076979)
Look into "Row types" and how PureScript, Haskell, and Elm (to a limited extent) do it.

'{foo :: Int | bar} is a record with a known property 'foo' and some unspecified properties 'bar'. You cannot pass a `{foo :: Int, bar :: Int}` into a function that expects `{foo :: Int}`.

A function that accepts any record with a field foo, changes foo, keeping other properties intact has the type

    {foo :: Int | bar} -> {foo :: Int | bar}
Re:Node.js adds experimental support for TypeScript by nyssos on Friday July 26, 2024 @07:32AM (#41077741)
> That's just a good practice in the general case: an intermediate type that fully described the data wouldn't have saved you from overwriting it unless you actually looked closely at the type signature.

The issue isn't that it got overridden, it's that it got overridden with a value of the wrong type. An intermediate type signature with `updatedAt` as a key will produce a type error regardless of the type of the corresponding value.

> I'd be very interested to know what you'd do to change the type system here to catch this.

Like the other commenter said, extensible records. Ideally extensible row types, with records, unions, heterogeneous lists, and so on as interpretations, but that seems very unlikely.

Re:Node.js adds experimental support for TypeScript by cstrahan on Thursday July 25, 2024 @11:18AM (#41069695)
No, TypeScript is not unsound because it uses structural typing.

A language has a sound type system if every well-typed program behaves as defined by the language's semantics during execution.

Go is structurally typed, and yet it is sound: code that successfully type checks is guaranteed to abide the semantics of the language.

TypeScript is unsound because code that type checks does not necessarily abide the semantics of the language:

  function messUpTheArray(arr: Array<string | number>): void {
      arr.push(3);
  }
  
  const strings: Array<string> = ['foo', 'bar'];
  messUpTheArray(strings);
  
  const s: string = strings[2];
  console.log(s.toLowerCase())
`strings` is declared as a `Array<string>`, but TypeScript is happy to insert a `number` into it. This is a contradiction, and an example of unsoundness.

`s` is declared as `string`, but TypeScript is happy to assign a `number` to it. This is a contradiction, and an example of unsoundness.

This code eventually fails at runtime when we try to call `s.toLowerCase()`, as `number` has no such function.

What we're seeing here is that TypeScript will readily accept programs which violate its own rules. Any language that does this, whether nominally typed or structurally typed, is unsound.

Re:Node.js adds experimental support for TypeScript by jaggederest on Thursday July 25, 2024 @01:06AM (#41064900)
Yeah I start projects by explicitly typing `any` all over the place and gradually refining things, so every type that's specified is explicit and checked, I'm really enjoying that style.
Re:Node.js adds experimental support for TypeScript by crabmusket on Thursday July 25, 2024 @01:52AM (#41065119)
Combine this with an eslint config that nudges you about explicit any, and the typescript compiler option to disallow implicit any, and you're well taken care of.
Re:Node.js adds experimental support for TypeScript by black3r on Thursday July 25, 2024 @04:50AM (#41066362)
Or you can configure the TS compiler to allow JS imports, then everything also compiles and works, but you can slowly convert your codebase from JS to TS file by file and be sure that all TS files are properly typed and all JS files are untyped instead of having everything as TS files where some are typed and some are not.
Re:Node.js adds experimental support for TypeScript by Humphrey on Thursday July 25, 2024 @01:30AM (#41065021)
Exactly. And if you use a library that does lots of meta programming (like Django) then it's impossible to pass all type errors. Hopefully one day the type system will be powerful enough to write a Django project with passing tests.
Re:Node.js adds experimental support for TypeScript by pansa2 on Thursday July 25, 2024 @01:25AM (#41064992)
With this approach, do you still use Python's standard syntax for type hints?

    def mersenne(p: int): return 2**p - 1
Or, given there's no need for the type hints to be checker-friendly, do you make them more human-friendly, e.g:

    def mersenne(p: 'prime number'): return 2**p - 1
Re:Node.js adds experimental support for TypeScript by LordKeren on Friday July 26, 2024 @09:44AM (#41078612)
While it’s not common (from the source code I’ve reviewed over the years), some people make a new type with a name and use that in the definition:

```

from typing import NewType

# Create a new type for some_prime SomePrime = NewType('SomePrime', int)

def process_prime(value: SomePrime) -> int: return value ```

However, this isn’t nearly as common as simply using a more descriptive argument name like “prime_number : int”

One of the big advantages to type hinting in Python is that it feeds the IDE a lot of information to increase auto-complete functionality, so you want to avoid things like p:”prime number”

Re:Node.js adds experimental support for TypeScript by pansa2 on Friday July 26, 2024 @11:57AM (#41079668)
> One of the big advantages to type hinting in Python is that it feeds the IDE a lot of information to increase auto-complete functionality

Yeah, until this discussion I thought the main benefit of type hints was earlier detection of bugs via static checking. Now though, I'm getting the impression that the bigger benefit is enabling IDE features such as autocomplete.

That helps me understand better why I haven't found type hints as useful as others seem to - I don't use an IDE. My use of Python is limited to small scripts which I write in a simple text editor.

Re:Node.js adds experimental support for TypeScript by LordKeren on Friday July 26, 2024 @12:36PM (#41079990)
There’s a fair argument to make that better IDE autocomplete does prevent many type-related issues even before a static type checker is run
Re:Node.js adds experimental support for TypeScript by edflsafoiewq on Thursday July 25, 2024 @08:11AM (#41067684)
IME if you aren't checking them, they're eventually going to be out of date.
Re:Node.js adds experimental support for TypeScript by rlt on Thursday July 25, 2024 @03:43PM (#41072580)
I don't find TypeScript to be burdensome when rapidly iterating. Depending on how you've configured your dev environment you can just ignore type errors and still run the code.