diff --git a/README.md b/README.md index 4a51e89..af5427b 100644 --- a/README.md +++ b/README.md @@ -647,10 +647,71 @@ type C = Omit; 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 + +```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.