Live data from Hacker News

Functional Programming with TypeScript's Type System

desislav.dev

11–20 of 46 posts

Re: Functional Programming with TypeScript's Type System

#11

While no actual Turing machine’s tape is infinitely long, I found issues in TypeScript with how finite generics are. You have to define every possible count of generic arguments if you want to preserve their types. And if you go above that count your type system degrades. I think there’s also a maximum of 7 or so before it doesn’t work. Beyond that and the generic type widens. For example, Lodash enumerating types fo…

This was fixed in Typescript 4.0, with the introduction of variadic tuples: https://www.typescriptlang.org/docs/handbook/release-notes/t...

Oh awesome! Thanks for sharing.

Re: Functional Programming with TypeScript's Type System

#14
I don't get TypeScript's type system.

It is obviously very powerful and can model very complex type constraints.

But then you have stuff like this where it is not checking types as I would expect:

  interface Foo { bar: string; }
  const f = {bar: "foobar"} as Readonly;
  function someFunc(): Foo {
    return f; // No error or warning, even with all strict flags enabled
  }

Re: Functional Programming with TypeScript's Type System

#15
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Sorry, I don't get it. What do you expect to happen here?

Re: Functional Programming with TypeScript's Type System

#17
post #15
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Sorry, I don't get it. What do you expect to happen here?

Presumably the issue is that a Readonly shouldn't be a subtype of Foo

I should note that I haven't yet had the pleasure of using a language that handles const-ness properly, as Readonly should be neither a subtype nor a supertype of T

Re: Functional Programming with TypeScript's Type System

#18
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

Handling of readonly correctness is definitely something ts doesn't handle well

Re: Functional Programming with TypeScript's Type System

#20
post #14

I don't get TypeScript's type system. It is obviously very powerful and can model very complex type constraints. But then you have stuff like this where it is not checking types as I would expect: interface Foo { bar: string; } const f = {bar: "foobar"} as Readonly ; function someFunc(): Foo { return f; // No error or warning, even with all strict flags enabled }

This is an issue open for discussion since 2017

https://github.com/microsoft/TypeScript/issues/13347

Post reply on HN