Created
April 5, 2024 00:24
-
-
Save im4aLL/98e707d280afb968e56a9309d990d510 to your computer and use it in GitHub Desktop.
cool items in typescript
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
type UserUrl = string; | |
// const getUserUrl = (id: number) => { | |
// return `/user/${id}`; // as const | |
// }; | |
// type UserUrl = ReturnType<typeof getUserUrl>; | |
const url: UserUrl = '/user/2'; |
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
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; | |
} | |
} |
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
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 |
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
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]; | |
// } & {}; |
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
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