Hacker News new | past | comments | ask | show | jobs | submit login

> Is it actually unsound if you perform no casting?

Yeah, it is. At the very least, I know that function parameters are bivariant, which isn't sound:

    function foo(callback: (obj: Object) => void) {
      callback("not a bool");
    }

    function main() {
      foo(function (bool: Boolean) {
        if (bool) window.console.log("!");
      });
    }
This compiles without a type error but ends up assigning a string to a parameter of type bool.



I'm a Flow user rather than TS (and Flow complains about that code out of the box), but you are not telling us all:

Since TS 2.6, if you enable all the optional "strict" mode checks than TypeScript does complain about that code.

I see no disadvantage in those checks, e.g. "strictFunctionTypes", being optional, since you can always make them mandatory for your own project(s).

To test it:

- Go to the TS Playground at https://www.typescriptlang.org/play/

- Enter the above code

- Select "Options" and enable at least "strictFunctionTypes"


Is that option documented somewhere? I imagine there are some tradeoffs with enabling it.


https://www.typescriptlang.org/docs/handbook/release-notes/t...

> TypeScript 2.6 introduces a new strict checking flag, --strictFunctionTypes.

> The --strictFunctionTypes switch is part of the --strict family of switches, meaning that it defaults to on in --strict mode.

> Under --strictFunctionTypes function type parameter positions are checked contravariantly instead of bivariantly.

> ...(etc.)


Contravariance then, right. Thanks! I haven't gotten into Typescript yet, and at this point it looks fairly large and a little daunting, so this is helpful.


And this project is super interesting: https://1c.wizawu.com/#/whatis Use TypeScript on JVM with both NPM and Maven dependencies


The major tradeoff, IMO, is that it is even toggle-able at all, let alone being a feature switch that is enabled by default.

If you enter any random TS project today, odds are it isn't using that switch, and enabling the switch will lead you down a month long rabbit-hole of fixing cascading type errors.


Not only that, but since it isn't required many libraries in the ecosystem won't be using it. Comparing my experiences with TypeScript and Flow vs. my experiences with Haskell, any optional type system is going to leave a lot of gaps if you don't have a pretty strict not-invented-here culture.


Tradeoffs? It might frustrate some people more accustomed to JS' loose type system.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: