-
Notifications
You must be signed in to change notification settings - Fork 78
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class properties #59
Open
fizker
wants to merge
2
commits into
niieani:master
Choose a base branch
from
fizker:private-class-properties
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Class properties #59
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -647,10 +647,71 @@ type C = Omit<A, B>; | |
|
||
However, Flow implementation is stricter in this case, as B have a property that A does not have, it would rise an error. In Typescript, however, they would be ignored. | ||
|
||
## Private properties in classes | ||
|
||
### flow | ||
|
||
```js | ||
class SomeClass { | ||
prop: string | ||
#prop2: string | ||
#prop3: string = "default value" | ||
|
||
constructor(prop: string, prop2: string) { | ||
this.prop = prop | ||
this.#prop2 = prop2 | ||
} | ||
} | ||
``` | ||
|
||
### TypeScript | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example can be removed, or moved to the part below (above Object callable property), since it duplicates the part that starts with:
|
||
|
||
```ts | ||
class SomeClass { | ||
constructor(public prop: string, private prop2: string) { | ||
// transpiles to: | ||
// this.prop = prop; | ||
// this.prop2 = prop2; | ||
} | ||
private prop3: string = "default value"; | ||
} | ||
``` | ||
|
||
# Same syntax | ||
|
||
Most of the syntax of Flow and TypeScript is the same. TypeScript is more expressive for certain use-cases (advanced mapped types with keysof, readonly properties), and Flow is more expressive for others (e.g. `$Diff`). | ||
|
||
## Public class properties | ||
|
||
Both systems support public class properties: | ||
|
||
```js | ||
class SomeClass { | ||
prop: string | ||
|
||
a() : string { | ||
return this.prop | ||
} | ||
|
||
constructor(prop:string) { | ||
this.prop = prop | ||
} | ||
} | ||
``` | ||
|
||
Also, TypeScript have some syntactic sugar for setting the props through the constructor. The following TypeScript snippet is equivalent to the snippet above: | ||
|
||
```ts | ||
class SomeClass { | ||
a() : string { | ||
return this.prop | ||
} | ||
|
||
constructor(public prop:string) { | ||
} | ||
} | ||
``` | ||
|
||
## Object callable property | ||
|
||
The basic syntax are the same, except Flow has special syntax for the internal call property slot. | ||
|
@@ -810,19 +871,6 @@ function something(this: { hello: string }, firstArg: string) { | |
} | ||
``` | ||
|
||
## Private and Public properties in classes | ||
|
||
```ts | ||
class SomeClass { | ||
constructor(public prop: string, private prop2: string) { | ||
// transpiles to: | ||
// this.prop = prop; | ||
// this.prop2 = prop2; | ||
} | ||
private prop3: string; | ||
} | ||
``` | ||
|
||
## [Non-null assertion operator](https://github.com/Microsoft/TypeScript/pull/7140) | ||
|
||
Add `!` to signify we know an object is non-null. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example below isn't
flow
specific. It works the same way as in TypeScript.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this could be specified as "same syntax" (without separation), then I'll accept.