Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Created April 5, 2024 00:24
Show Gist options
  • Save im4aLL/98e707d280afb968e56a9309d990d510 to your computer and use it in GitHub Desktop.
Save im4aLL/98e707d280afb968e56a9309d990d510 to your computer and use it in GitHub Desktop.
cool items in typescript
type UserUrl = string;
// const getUserUrl = (id: number) => {
// return `/user/${id}`; // as const
// };
// type UserUrl = ReturnType<typeof getUserUrl>;
const url: UserUrl = '/user/2';
const CONFIG = {
pumpType: 'conventional',
pumpName: 'Sample pump',
};
// as const
class Example {
public config = CONFIG;
constructor() {
this.config.pumpName = 'something else';
}
getConfiguration(): typeof CONFIG {
return this.config;
}
}
interface IField {
name: string;
age: number;
}
type FormValue = Record<
'name' | 'age',
{
label: string;
message: string;
}
>;
const formValues: FormValue = {
name: {
label: 'Your name',
message: 'Your full name',
},
age: {
label: 'Your age',
message: 'you have to be above 18+',
},
};
// keyof IFields
interface IUser {
name: string;
age: string;
}
interface IAdminUser extends IUser {
isAdmin: boolean;
}
interface ISuperUser extends IAdminUser {
isSuperUser: boolean;
}
const user: ISuperUser = null;
// type Prettify<T> = {
// [K in keyof T]: T[K];
// } & {};
interface Product {
name: string;
}
interface Product {
price: number;
}
const product: Product = {
name: 'Apple',
price: 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment