Skip to content

Instantly share code, notes, and snippets.

@TorbjornHoltmon
TorbjornHoltmon / PartialBy.ts
Last active Dec 1, 2021
PartialBy, make properties in typescript type optional
View PartialBy.ts
/**
* Construct a type with the properties of T and make the properties K optional.
*/
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
// Example:
interface Person {
name: string;
age: number;
gender: "male" | "female";
View awaitableTimout.ts
async function voidWait(timeToDelay: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, timeToDelay));
}
async function returnWait<T>(timeToDelay: number, returnValue: T): Promise<T> {
return new Promise((resolve) => setTimeout(resolve(returnValue), timeToDelay));
}
@TorbjornHoltmon
TorbjornHoltmon / getWeekNumberFromISOString.ts
Last active Nov 23, 2021
Get week number from ISO string typescript
View getWeekNumberFromISOString.ts
export function getWeekNumberFromISOString(date: string): number {
const newYear = new Date(new Date(date).getFullYear(), 0, 1);
let day = newYear.getDay() - 1; // the day of week the year begins on
day = day >= 0 ? day : day + 7;
const dayNumber: number =
Math.floor(
(new Date(date).getTime() -
newYear.getTime() -
(new Date(date).getTimezoneOffset() - newYear.getTimezoneOffset()) * 60000) /
86400000