Created
July 9, 2020 20:33
-
-
Save stephenh/eb95673ef194aa7fcd873f28e036ab94 to your computer and use it in GitHub Desktop.
ExactType
This file contains 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 Args = { | |
id?: string | undefined | null; | |
firstName?: string | undefined | null; | |
parentId?: string | undefined | null; | |
different?: string | undefined | null; | |
}; | |
type Opts = { | |
id?: string | undefined | null; | |
firstName?: string | undefined | null; | |
parent?: string | undefined | null; | |
}; | |
const a: Args = null!; | |
// I want parentId and different to be type failures | |
const o: Opts = a; | |
function doOpts(o: Opts): void {} | |
function onlyOpts<T extends Opts>(o: Exact<Opts, T>): void {} | |
function onlyOpts2<O>(o: Exact<Opts, O>): void { | |
doOpts(o); | |
} | |
onlyOpts(a); | |
onlyOpts2(a); | |
onlyOpts({ firstName: "as" }); | |
onlyOpts({ parent: "as" }); | |
type Impossible<K extends keyof any> = { | |
[P in K]: never; | |
}; | |
// T is the exact type, U is the user's passed type, | |
type Exact<T, U> = T & U & Impossible<Exclude<keyof U, keyof T>>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment