Skip to content

Instantly share code, notes, and snippets.

@codersidprogrammer
Created June 17, 2024 01:21
Show Gist options
  • Save codersidprogrammer/2a8603b42394ccd3cffa4760ddd53e85 to your computer and use it in GitHub Desktop.
Save codersidprogrammer/2a8603b42394ccd3cffa4760ddd53e85 to your computer and use it in GitHub Desktop.
// Part 1
let kota:string;
let tahun:number;
let isActive: boolean;
kota = 'bandung';
tahun = 2024;
isActive = true;
// Part 2
type GabunganDuaData = string | number; // definisi type data baru
let contohData: GabunganDuaData = 2;
type JenisKelamin = 'laki' | 'perempuan';
let Person: {gender: JenisKelamin} = {
gender: 'laki',
}
// Part 3: interface
interface PersonInterface {
nama: string;
age: number;
sayHello(): string;
}
// implementasi dalam object
const Anton: PersonInterface = {
nama: "Anton",
age: 25,
sayHello: function(): string {
return `Hello, my name is ${this.nama}!`
}
}
// Implementasi dalam class OOP: Service
type LoginStatus = 'login' | 'logout';
interface AuthenticationFlow {
getCredential(user: string, pass: string): boolean;
checkLoginStatusByUsername(user: string): LoginStatus;
}
// Implementasi menggunakan SSO
class SSOService implements AuthenticationFlow {
getCredential(user: string,pass: string): boolean {
if (user === 'dimas' && pass === '123456') {
return true;
}
return false;
}
checkLoginStatusByUsername(user: string): LoginStatus {
if (user === 'dimas') {
return 'login';
}
return 'logout';
}
}
// Implementasi menggunakan Internal API
class LoginService implements AuthenticationFlow {
getCredential(user: string,pass: string): boolean {
if (user === 'admin' && pass === 'nimda') {
return true;
}
return false;
}
checkLoginStatusByUsername(user: string): LoginStatus {
if (user === 'admin') {
return 'login';
}
return 'logout';
}
}
class LoginInternalService {}
// Use case
const LoginProvider: AuthenticationFlow = new LoginService();
// Implementasi dalam DTO (Generic)
/**
*
* // return dari pagination of list person
*
* {
* data: [
* {name: 'dimas', age: 17},
* {name: 'anton', age: 17},
* ],
* page: 1,
* totalPage: 10,
* }
*
* // return dari pagination of list todo
* {
* data: [
* {title: 'reading a book', status: true},
* {title: 'washing', status: false},
* ],
* page: 1,
* totalPage: 10,
* }
*
*/
interface PersonObject {
nama: string;
age: number;
}
interface TodoObject {
title: string;
status: boolean;
}
interface ApiResponse<T> {
data: T;
page: number;
totalPage: number;
}
const mockupPersonResponse: ApiResponse<Array<PersonObject>> = {
data: [
{nama: 'dimas', age: 17},
{nama: 'anton', age: 17}
],
page: 1,
totalPage: 10
}
const mockupTodoResponse: ApiResponse<Array<TodoObject>> = {
data: [
{title: 'reading a book', status: true},
{title: 'washing', status: false},
],
page: 0,
totalPage: 0
}
// Inheritance
// parent class
abstract class Animal {
abstract sound(): void;
getClassName(): string {
return 'Animal';
}
}
// child class cat
class Cat extends Animal {
sound(): void {
console.log('Meow');
}
}
// child class dog
class Dog extends Animal {
sound(): void {
console.log('Gog..');
}
override getClassName(): string {
return Dog.name;
}
}
const Moci: Animal = new Cat();
Moci.sound();
console.log(Moci.getClassName())
const Doris: Animal = new Dog();
Doris.sound();
console.log(Doris.getClassName())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment