Last active
June 28, 2019 18:20
-
-
Save hasparus/82e81d9a389fb8bc75c46c14a4551ec0 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
const questions = [ | |
{ | |
message: 'Enter your login (email address)...', | |
name: 'login', | |
type: 'input', | |
}, | |
{ | |
message: 'Enter your password...', | |
name: 'password', | |
type: 'password', | |
}, | |
] as const; | |
type Input = string & { __brand: 'Input' }; | |
type Password = string & { __brand: 'Password' }; | |
type QuestionTypeToValueType = { | |
input: Input; | |
password: Password; | |
price: number; | |
} | |
type Question = { | |
readonly message: string; | |
readonly name: string; | |
readonly type: string; | |
} | |
type Answer<Q extends Question> = {[P in Q['name']]: Q['type']}; | |
/** | |
* type X = { login: "input" }; | |
*/ | |
type X = Answer<{ message: '', name: 'login', type: 'input' }>; | |
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; | |
type Simplify<T> = Pick<T, keyof T> | |
type Answers<Qs extends readonly Question[]> = Simplify<UnionToIntersection<{ | |
[I in keyof Qs]: Qs[I] extends Qs[number] ? Answer<Qs[I]> : never; | |
}[number]>> | |
/** | |
* type Y = { | |
* login: 'input'; | |
* password: 'password'; | |
* } | |
*/ | |
type Y = Answers<typeof questions>; | |
// type Z = { | |
// login: Input; | |
// password: Password; | |
// } | |
type Z = { [K in keyof Y]: QuestionTypeToValueType[Y[K]] } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://typescript-play.js.org/#code/MYewdgzgLgBAjgVwKbQJbgjAvDA2gKBhgG9CiYBbFCAQwHMkAuGAcgFEwokAnGATxAJeAGxB1UYGAAokFGqmEwaAE2XdqASgB0OlgBoyRMDSrMWo8WH2GYUPgAcmrCfYRRrRAL4GipcpWp6J3ZOHn5BXnsaCAgAdxBuZR0tD3JjU1YomPjE1KI7RzMsuITlVO98AF0lTFBIKABufHwCpBgASTBXWBxobgk6GAAyEhgAfTGAI24aMGUzTu6WGE8m1pgABWiSxOwYPoHh0Ynp2fnWLezS5dXm9YBFZDRwABUHJBeQADUaYWQ3xx7PzkFxuZiLNxNfzFHLnS47ZRQ8j2frAJxgBAUSY8JqeO7vGCPFBQdCSHDAojqFTgYR8AIxILMA5gOhIylIalgWkwdJOZmsmxU5Q0umtJlQfos3H4wEAQUgsR4AB57jAkAAPLhzTBE55gAB8QNwGxgEkJuBYvJYlUqzHuFta1tu+AA9AAqN2EN22AkADSBMAsEmYACJQVAQysoW6XS0-Xt5XFlcR6bQGGZ9DyTMEg1Y9D7Cs4um4bvqmnHAQBVMCkz6dLjcCBIYAk8BKyuGnBSStqzVIbVKMB0gD80gA1sxKxpsIaAG4gVDKGDMMBIWc8acarXKTBSKQT01gABmYXa06wc4Xymno-ay55a5xMraAGVUBR7MJUEe+EqXp3NlQYAxz-fMxyQAQjxgf9nxgRNFUbFVMC3fsdxgIURUJJ5WzAXBKgAt8Py-H92xrV4QHrHgmxbUklQpPA7zNcDIMJCBbVY3B2mqFCB3uCBcAxLEeGqUd4OVPjOPw+9V3XbhcQEzFsW4fD9Wad1PRgb11gATSBL1yFzMxwxYaNkW2WEinM65TLxTTYx0hMFSopVWhAKDEGJUkIDLNSXQLNoAC09JdPz-EMjpi0aV1QrMq5EmYeFYSaEKVgrQKA1wABpQ8YGYtyYG09jdRwgEPm+X5-neXBtKym1UvwIA