Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

docs: add %checks and T is x#51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Bjeaurn wants to merge1 commit intoniieani:master
base:master
Choose a base branch
Loading
fromBjeaurn:patch-1

Conversation

Bjeaurn
Copy link

No description provided.

@niieani
Copy link
Owner

Hi@Bjeaurn. This feature is not unique to Flow, as TypeScript has an equivalent (and even more advanced/useful):T is SomeType return type.

I'd happily accept a description/comparison of both features into the readme.

styfle reacted with thumbs up emoji

@niieaniniieani changed the titledocs: Adds a missing unique feature from Flow to the list.docs: add %checks and T is xJan 19, 2019
@FezVrasta
Copy link
Contributor

(and even more advanced/useful)

This kind of comments make this project lose all its credibility, just FYI

@slikts
Copy link

Is the comment inaccurate? If so, you could elaborate

@FezVrasta
Copy link
Contributor

They do the same thing

@niieani
Copy link
Owner

niieani commentedJun 20, 2019
edited
Loading

They don't do the same thing@FezVrasta. You can't use%checks in declarations in all the cases where you can usex is T, making its usefulness limited.

Below is one really useful example taken from the TypeScript's library definition (Array.filter):

interfaceArray<T>{filter<SextendsT>(callbackfn:(value:T,index:number)=>value isS):S[];}

This makes is possible refine the type of an Array by filtering it.

You can't model this with%checks (or with Flow altogether). If you could, Flow's official library would do it (it doesn't).

Additionally,%checksdoesn't work in class methods.

I apologize for not being clear in my first comment, but my intention is completeness, not bias against any of the two technologies.

slikts, styfle, kangax, and andrekovac reacted with thumbs up emoji

@FezVrasta
Copy link
Contributor

How can you say theArray#filter TypeScript's type definition is correct? It expects the callbackFn to return a type extended fromT while it should accept any value and cast it toboolean 🤔

The Flow definition seems way more accurate, since it allows to returnany from the callback.

@niieani
Copy link
Owner

niieani commentedJun 20, 2019
edited
Loading

That's not the meaning ofvalue is T. The return value still has to be a boolean (just like with%checks).

The difference is in the fact that such definedboolean has extra meaning attached to it, in that it ensures the mapping between:true andvalue is T, versus in the case of Flow, it ensures the value is "checked" against the logic within the body of the function.

Here's an example comparing the two in respective playgrounds:TypeScript vsFlow

All in all,%checks is similar to TypeScript'sparam is T, with these exceptions:

  • %checks cannot appear in class methods (that's why it cannot be defined in theArray case)
  • you cannot mark a check with an arbitrary type in Flow - it only works based on the logic defined in the body of the function: i.e. things liketypeof,instanceof will work, but not complex cases where you reuse logic from a different function, or in cases where there is no body at all (e.g. library definitions))

The two reasons above make TypeScript'sparam is T more powerful and useful.

styfle and kangax reacted with thumbs up emoji

niieani added a commit that referenced this pull requestJun 20, 2019
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@Bjeaurn@niieani@FezVrasta@slikts

[8]ページ先頭

©2009-2025 Movatter.jp