my-awesome-typescript-project / src/basic-types
Basic types.
Defined in: src/basic-types.ts:18
Simple interface
- Defines object shape: properties names, types, order, optionality.
- Interfaces can be modified after creation but this should never be done.
| Property | Type | Defined in |
|---|---|---|
one |
string |
src/basic-types.ts:19 |
two? |
number |
src/basic-types.ts:20 |
three |
(s) => number |
src/basic-types.ts:21 |
four |
(s) => string |
src/basic-types.ts:22 |
Defined in: src/basic-types.ts:34
Extended interface
- Shows how the
extendclause is used declaratively in typescript. - Properties accept a
readonlymodifier. - A single interface can extend multiple interfaces (separated with commas).
| Property | Modifier | Type | Inherited from | Defined in |
|---|---|---|---|---|
one |
public |
string |
objectShape.one |
src/basic-types.ts:19 |
two? |
public |
number |
objectShape.two |
src/basic-types.ts:20 |
three |
public |
(s) => number |
objectShape.three |
src/basic-types.ts:21 |
four |
public |
(s) => string |
objectShape.four |
src/basic-types.ts:22 |
five |
readonly |
symbol |
- | src/basic-types.ts:35 |
type extendedType = extendedType;Defined in: src/basic-types.ts:46
Extended type
- Equivalent of extending an interface, actually intersection type of both constituents.
- Reminder: a subtype always have more or more specific properties than its supertype.
Defined in: src/basic-types.ts:57
Create interface from implementation
- This pattern can be used to extend built-in objects and override their methods.
| Property | Modifier | Type | Inherited from | Defined in |
|---|---|---|---|---|
five |
readonly |
symbol |
- | src/basic-types.ts:58 |
one |
public |
string |
objectClass.one |
src/basic-types.ts:101 |
two |
public |
number |
objectClass.two |
src/basic-types.ts:102 |
three |
public |
(s) => number |
objectClass.three |
src/basic-types.ts:103 |
four(s): string;Defined in: src/basic-types.ts:110
| Parameter | Type |
|---|---|
s |
string |
string
const objectLiteral: objectShape;Defined in: src/basic-types.ts:67
Simple object literal
- The object keys and values conform to the interface.
const derivedLiteral: derivedShape;Defined in: src/basic-types.ts:80
Derived object literal
- Same as
export const derivedLiteral: objectShape & extendedShape.
Defined in: src/basic-types.ts:100
Simple class
- The class implements the interface.
- Important: interface properties define public contracts only.
- An alternative is to use the interface to declare getters and setters of private properties.
- Reminder:
public,private,protectedandreadonlymodifiers are typescript only and not supported by native ECMA. - When in doubt, refer to the list of ECMA reserved words for javascript.
- Personal note : the following interpretation involuntarily makes a case for using functional programming over classes.
new objectClass(): objectClass;Defined in: src/basic-types.ts:104
| Property | Type | Defined in |
|---|---|---|
one |
string |
src/basic-types.ts:101 |
two |
number |
src/basic-types.ts:102 |
three |
(s) => number |
src/basic-types.ts:103 |
four(s): string;Defined in: src/basic-types.ts:110
| Parameter | Type |
|---|---|
s |
string |
string
Defined in: src/basic-types.ts:124
Derived class
- Illustrates ECMA
extendsvs typescriptimplements. - Overridden methods have to be identified and cannot violate the superclass types.
super.someMethod()can be used in overriden methods to access superclass method.
new derivedClass(): derivedClass;Defined in: src/basic-types.ts:126
| Property | Type | Inherited from | Defined in |
|---|---|---|---|
one |
string |
extendedShape.one objectClass.one |
src/basic-types.ts:101 |
two |
number |
extendedShape.two objectClass.two |
src/basic-types.ts:102 |
three |
(s) => number |
extendedShape.three objectClass.three |
src/basic-types.ts:103 |
five |
symbol |
- | src/basic-types.ts:125 |
four(s): string;Defined in: src/basic-types.ts:131
| Parameter | Type |
|---|---|
s |
string |
string
const literal: {
a: 0;
b: "union";
};Defined in: src/basic-types.ts:148
Create literal type from implementation
| Name | Type | Default value | Defined in |
|---|---|---|---|
a |
0 |
0 |
src/basic-types.ts:148 |
b |
"union" |
- | src/basic-types.ts:148 |
- Converting an object to a literal type using
as constmakes all its properties read-only.