Last active
May 13, 2020 11:36
-
-
Save marekdano/f67e06bb49bc26c34f6074620fbe6f6b to your computer and use it in GitHub Desktop.
TS samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fail(arr: never): never { | |
return new Error; | |
} | |
function pluck<T, K extends keyof T>(obj: T, propertyNames: K[]): T[K][] { | |
return propertyNames.map(key => obj[key]) | |
} | |
interface Author { | |
name: string; | |
} | |
interface Course = { | |
id: number; | |
title: string; | |
author: number; | |
} | |
const course: Course = { | |
id: 123, | |
title: 'Advanced Typescript', | |
author: { | |
name: 'Marek Dano' | |
}, | |
} | |
pluck(course, ['name]) // error | |
pluck(course, ['title]) // OK | |
type Freeze<T> = { | |
readonly [P in keyof T]: T[P]; | |
} | |
const freezeObj = Freeze<Course> = course; | |
// OR use built-in function Readonly | |
const freezeObj = Readonly<Course> = course; | |
freezeObj.title = ''; // error - can't change property | |
interface Mentor { | |
readonly name: string; | |
readonly course: Course; | |
} | |
type Writable<T extends {[key: string]: any}, K extends string> = { | |
[P in K]: T[P] | |
} | |
const writableMentor: Writable<Mentor, keyof Mentor> = {name: 'davy', course}; | |
writableMentor.course = course; // works OK | |
// OR use | |
type Writable<T> = { | |
-readonly [P in keyof T]: T[P]; | |
} | |
const writableMentor: Writable<Mentor> = {name: 'davy', course}; | |
writableMentor.course = course; // works OK | |
// Overloading functions | |
export function findCourse(name: string): Course[]; | |
export function findCourse(id: number): Course[]; | |
// above this can be replaced by | |
export function findCourse <T>(args: T): Course[]; | |
export function findCourse (author: Author): Course[]; | |
export function find<T, U>(args: T): U[] // example of using generics | |
export function findCourse(searchTerm: number | string | Author): Course[] { | |
if (typeof searchTerm === 'string') { | |
return courses.filter(course => course.title === searchTerm); | |
} | |
if (typeof searchTerm === 'number') { | |
return courses.filter(course => course.id === searchTerm); | |
} | |
return courses.filter(course => course.author.name.includes(searchTerm)); | |
} | |
findCourse<number>(123); | |
findCourse<string>('Advanced Typescript'); | |
// conditional type below | |
type NoNullable<T> = T extends null | undefined ? never : T; | |
function transform<T extends string>(text: T): T extends string ? string : null; | |
function transform(text: string | null): string | null { | |
return text && text.replace(/f/g, 'p'); | |
} | |
interface Cat {} | |
interface Lion {} | |
interface Sharp { | |
swim: boolean; | |
} | |
type Animal = Cat | Lion | Shark; | |
type ExtractFish<A> = A extends { swim: boolean } ? A : never; | |
type Fish = ExtractFish<Animal>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment