Created
July 20, 2025 09:27
-
-
Save reznik789/9e7146d559c830021b276a28bc675af6 to your computer and use it in GitHub Desktop.
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 Equals<A, B> = | |
(<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) | |
? true | |
: false; | |
type ReadOnlyObj<T, K extends keyof T> = { | |
readonly [P in K]: T[K] | |
} | |
type IsReadonly<T, K extends keyof T> = Equals<ReadOnlyObj<T, K>, {[P in K]: T[K]}> extends true ? 'readonly' : 'writable'; | |
// чтобы | |
interface Foo { | |
bar: string; | |
readonly baz: string; | |
} | |
type A = IsReadonly<Foo, 'bar'>; // 'writable' | |
type B = IsReadonly<Foo, 'baz'>; // 'readonly' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment