facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.07k stars 1.85k forks source link

flow strict should allow unclear types for generics #9138

Closed ayroblu closed 5 months ago

ayroblu commented 5 months ago

Proposal

With the removal of inexact types for generics matching exact types in the recent versions, we should have an easy way to specify Function, Object types in Generics while in @flow strict

type Gen<T: Object> = ...

Use case

We use @flow strict to reduce the usages of any types. However, for generics, we want to say that something is struct Object. Before we would just use {} (or {...}) but that's now gone, we would like a way to say something is an Object without receiving the wrath of the checker that we're doing something wrong

SamChou19815 commented 5 months ago

Before we would just use {} (or {...}) but that's now gone

What do you mean by this? {...} is a good upper bound for all objects, if you want to include interfaces as well as objects, you can use interface {}

This page contains information you can find to specify the top types: https://flow.org/en/docs/lang/type-hierarchy/

ayroblu commented 5 months ago

try flow

SamChou19815 commented 5 months ago

Arrays are invariantly typed, so you need to use $ReadOnlyArray instead

ayroblu commented 5 months ago

Invariants don't let you type function inputs though?

try flow

Or functions generally:

try flow

SamChou19815 commented 5 months ago

For functions parameters, you will need contra-variance -. Note that ({...}) => mixed doesn't admit ({foo: string}) => mixed, since a function that calls f: ({...}) => mixed cannot be passed a callback that can only handle {foo: string} objects.

SamChou19815 commented 5 months ago

The unclear-type error is there because these types are just any, which is inherently unsafe. It's conceptually the same as this lint rules, except that it's built in the checker itself. If you are unhappy with it, you can disable the lint