Last active
November 21, 2023 09:12
-
-
Save ethansnow2012/7ef46f9d0cf45401aad84b4fd368b321 to your computer and use it in GitHub Desktop.
Typescript: SameType
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
/** | |
* 由其他generic type組成的會優先保持generic的描述,這樣無法“直接”看到單一property的值。 | |
* 用這個解構就可以看到property/value的值,debug或查詢蠻方便的。 | |
*/ | |
type FlatPrettified<T extends any> = { | |
[P in keyof T]:T[P] | |
} | |
/** | |
* 改變型別的Optional設定,在資料流的旅程中,有部分的路線是Optional的但又不想放寬整個型別設定。 | |
*/ | |
type MakeSomeFieldsOptional<T, Fields extends keyof T> = Omit<T, Fields> & | |
Partial<Pick<T, Fields>>; | |
/** | |
* 改變型別特定property的型別 | |
*/ | |
type ChangeFieldTypes<T, Fields, NewType> = { | |
[P in keyof T]: P extends Fields ? NewType : T[P]; | |
}; | |
/** | |
* type representation of an array without using an array(but they are fundamentally differerent. Array come with things like length property and method) | |
**/ | |
let aa: Record<number, string> = ['1','2','3'] | |
let bb:{ [P in number]: string } = ['1','2','3'] | |
type myArray<T=any> = { [P in number]: T } | |
let cc:myArray = ['1','2','3'] as unknown as [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment