About me
thatâs me!
www.typescriptlang.org/docs
www.typescriptlang.org/docs
www.typescriptlang.org
www.typescriptlang.org
www.typescriptlang.org
đ¤
Scale?�You mean like big data
Margaret Hamilton stands next to a stack of Apollo Guidance Computer source code.
Credits: MIT Museum
đ
Gotcha.�So how do we make our code scale?
Scalable code
Scalable code
âThe ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write.â
â Robert C. Martin Clean Code: A Handbook of Agile Software Craftsmanship
đ
Will you get to code examples at some point?
const getInformation = function(data) {
// ...
}
const getInformation = function(data) {
// ...
}
const getInformation = function(data: SegmentData): Information | undefined {
// ...
}
const getInformation = function(data) {
// ...
}
const getInformation = function(data: SegmentData): Information | undefined {
// ...
}
const getInformation = function(data: any): any {
// ...
}
class SegmentClassicOop extends BaseObject {
private name: string
private description: string
private folderId: number
private rule: Rule
private tenantId: number
public createSegment(name: string, description: string, folderId: number, ďż˝ rule: Rule, tenantId: number) {
if (name.length < 5){
throw new Error('segment name is invalid')
}
if (!this.isRuleValid(rule)){
throw new Error('segment must have valid rule')
}
if (!tenantId) {
throw new Error('segment must have valid rule')
}
}
// ... Other methods
// ... getters and setters
}
class SegmentOop extends BaseObject {
public name: string
public description: string
public folderId: number
public rule: Rule
public tenantId: number
}
class RuleValidator {
public ruleValidator(){
//implement rule validation
}
}
// ... other validators
type Segment = BaseObject & {
name: string;
description: string;
folderId: number;
rule: Rule;
tenantId: number;
}
function ruleValidator(rule: Rule): boolean {
// implement validation
}
Data objects go here
Data objects go here
Behavior is separated by who/what/how/where
type Props = {
a: number;
b: string;
}
const props: Props = {
a: 3,
b: 'ok',
c: 'not ok'
}
// TS2322: ... c does not exist in type Props
ReadOnly
type Todo = {
title: string;
}
const todo: Readonly<Todo> = {
title: "Delete inactive users"
}
todo.title = "Hello";
// Cannot assign to 'title' because it is a read-only property
Required
type Props = {
a?: number;
b?: string;
}
const obj: Props = { a: 5 };
const obj2: Required<Props> = { a: 5 };
// Property 'b' is missing in type '{ a: number; }' ďż˝// but required in type 'Required<Props>'
Pattern - CQS
�export interface Command<Entity>{
execute(argument: Entity): void
}
export interface Query<QueryParams,Entity>{
execute(queryParams: QueryParams): Promise<Entity[]>
}
ďż˝
Convention - Controller
export type ExpressFunc = (req: express.Request, ďż˝ res: express.Response,ďż˝ next: express.NextFunction) => Promise<express.Response>
export type controller = {
getSingle: ExpressFunc,
get: ExpressFunc,
post: ExpressFunc,
put: ExpressFunc,
delete: ExpressFunc,
bulkDelete: ExpressFunc,
bulkPost: ExpressFunc,
bulkPut: ExpressFunc
}
export function restController<Query,DTO,Entity>(
getMethod: query<Query, Entity>,
postMethod: command<Entity>,
putMethod: command<Entity>,
deleteMethod: command<Entity>,
converter: converter<DTO,Entity>): controller {
return {
getSingle: runQuerySingle<Query, DTO, Entity>(getMethod, converter),
get: runQuery<Query, DTO, Entity>(getMethod, converter),
post: executeCommand<DTO, Entity>(postMethod, converter),
put: executeCommand<DTO, Entity>(putMethod, converter),
delete : executeCommand<DTO, Entity>(deleteMethod, converter),
bulkDelete : executeBulkCommand<DTO, Entity>(deleteMethod, converter),
bulkPost: executeBulkCommand<DTO, Entity>(postMethod, converter),
bulkPut: executeBulkCommand<DTO, Entity>(putMethod, converter)
}
}
đ¤
WOW! �Sounds Amazing
đ§
But wait.. �what about all the overhead typescript adds
Dealing with Overhead
Dealing with Overhead
const segment: Segment = getSegment(id)
const segment = getSegment(id)
Dealing with Overhead
Dealing with Overhead
Dealing with Overhead
Dealing with Overhead
type Segment = BaseObject & {
name: string;
description: string;
folderId: number;
rule: Rule;
tenantId: number;
}
function ruleValidator(rule: Rule): boolean {
// implement validation
}
enum ColorsEnum {
white = '#ffffff',
black = '#000000',
}
type Colors = keyof typeof ColorsEnum
let colorLiteral: Colors
colorLiteral = "white" // OK
colorLiteral = "black" // OK
colorLiteral = "red" // Error...
â redux.js.org/usage/usage-with-typescript
Scalability Test
đŹ
How confident?�Well I pray a lot before I deploy...
Trophy
Static
Unit
Integration
E2E
âTypeScript can catch errors and bugs at build time, long before your app goes liveâ
â https://reactjs.org/docs/static-type-checking
describe('segment use cases', () => {
test('should update segment', async () => {
const mockSegment = createMockSegment()
const savedSegmentId = await createSegment(mockSegment)
const savedSegment = await getSegment(mockSegment.savedSegmentId)
expect(savedSegment.name).toEqual(mockSegment.name)
expect(savedSegment.description).toEqual(mockSegment.description)
// you get the picture
})
//...more tests
describe('segment use cases', () => {
test('should update segment', async () => {
const mockSegment = createMockSegment()
const savedSegmentId = await createSegment(mockSegment)
const savedSegment = await getSegment(mockSegment.savedSegmentId)
expect(savedSegment.name).toEqual(mockSegment.name)
expect(savedSegment.description).toEqual(mockSegment.description)
// you get the picture
})
//...more tests
async function createMockSegment(): SegmentBO {
// âŚ
}
Summary
All You Need is Types!
All You Need is Types!
(And Generics)
All You Need is Types!
(And Generics)
(And Unions/Intersections)
All You Need is Types!
(And Generics)
(And Unions/Intersections)
(And TypeUtils)
All You Need is Types!
(And Generics)
(And Unions/Intersections)
(And TypeUtils)
(And a couple of other stuff but you get the picture)
@TalJoffe