Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 61 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

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.

Copy link
Owner

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.


```js
class SomeClass {
prop: string
#prop2: string
#prop3: string = "default value"

constructor(prop: string, prop2: string) {
this.prop = prop
this.#prop2 = prop2
}
}
```

### TypeScript
Copy link
Owner

Choose a reason for hiding this comment

The 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:

Also, TypeScript have some syntactic sugar for setting the props through the constructor


```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.
Expand Down Expand Up @@ -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.
Expand Down