Skip to content

Latest commit

 

History

History
342 lines (206 loc) · 12.5 KB

File metadata and controls

342 lines (206 loc) · 12.5 KB

my-awesome-typescript-project


my-awesome-typescript-project / src/basic-types

src/basic-types

Basic types.

Table of contents

1. Object shapes

objectShape

Defined in: src/basic-types.ts:18

Simple interface

Remarks

Extended by

Properties

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

extendedShape

Defined in: src/basic-types.ts:34

Extended interface

Remarks

  • Shows how the extend clause is used declaratively in typescript.
  • Properties accept a readonly modifier.
  • A single interface can extend multiple interfaces (separated with commas).

Extends

Properties

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

extendedType

type extendedType = extendedType;

Defined in: src/basic-types.ts:46

Extended type

Remarks

  • Equivalent of extending an interface, actually intersection type of both constituents.
  • Reminder: a subtype always have more or more specific properties than its supertype.

derivedShape

Defined in: src/basic-types.ts:57

Create interface from implementation

Remarks

Extends

Properties

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

Methods

four()
four(s): string;

Defined in: src/basic-types.ts:110

Parameters
Parameter Type
s string
Returns

string

Inherited from

objectClass.four

2. Object implementations

objectLiteral

const objectLiteral: objectShape;

Defined in: src/basic-types.ts:67

Simple object literal

Remarks

  • The object keys and values conform to the interface.

derivedLiteral

const derivedLiteral: derivedShape;

Defined in: src/basic-types.ts:80

Derived object literal

Remarks

  • Same as export const derivedLiteral: objectShape & extendedShape.

objectClass

Defined in: src/basic-types.ts:100

Simple class

Remarks

Extended by

Implements

Constructors

Constructor
new objectClass(): objectClass;

Defined in: src/basic-types.ts:104

Returns

objectClass

Properties

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

Methods

four()
four(s): string;

Defined in: src/basic-types.ts:110

Parameters
Parameter Type
s string
Returns

string

Implementation of

objectShape.four


derivedClass

Defined in: src/basic-types.ts:124

Derived class

Remarks

  • Illustrates ECMA extends vs typescript implements.
  • Overridden methods have to be identified and cannot violate the superclass types.
  • super.someMethod() can be used in overriden methods to access superclass method.

Extends

Implements

Constructors

Constructor
new derivedClass(): derivedClass;

Defined in: src/basic-types.ts:126

Returns

derivedClass

Overrides

objectClass.constructor

Properties

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

Methods

four()
four(s): string;

Defined in: src/basic-types.ts:131

Parameters
Parameter Type
s string
Returns

string

Implementation of

extendedShape.four

Overrides

objectClass.four

3. Literal types

literal

const literal: {
  a: 0;
  b: "union";
};

Defined in: src/basic-types.ts:148

Create literal type from implementation

Type Declaration

Name Type Default value Defined in
a 0 0 src/basic-types.ts:148
b "union" - src/basic-types.ts:148

Remarks

  • Converting an object to a literal type using as const makes all its properties read-only.