Last active
February 17, 2022 10:27
-
-
Save maxgfr/802040a46c190ba6ba7a6fc21ec286ac to your computer and use it in GitHub Desktop.
Type guard and type conversion 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
// https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeBYAUFTUwCcD2YAjAFxQDOwOAlgHYDm6W2+YATKRdfY1rgQMwdKtBmgC+6dKEhQAQgigB5ALZVgAHhgAaKAHI+bXQD5JaadADCClWs079LfsdPmoADXUAVKBAAewCBoAEzJYKAAfOQioCyMFbz8A4NC4AH4w0gT-QJCo9PlSCwBuUwBjPBoKKCoyC1IPWIUkKB5MAxIoACIQPE6tVuYCdi6AdwgyAAs+9Ak0dHLK4GqyWXr1WTjEZoH20m7e-oxeRz2yAEMAGwBXYGnxMoqqmpg1mE3kFqO2lg79u6YDMNOmNJv9jgJTpcbncxEA | |
type A = { | |
prop1: string | |
prop2: string | |
prop3: string | |
} | |
type B = Omit<A, 'prop2'> | |
type C = Omit<A, 'prop3'> | |
type X<T extends A | B | C> = T extends A ? A : T extends B ? B : C; | |
const isC: X<C> = { | |
prop1: "yo", | |
prop2: "wesh", | |
} | |
const isB: X<B> = { | |
prop1: "yo", | |
prop3: "salut", | |
} | |
const isA: X<A> = { | |
prop1: "yo", | |
prop2: "wesh", | |
prop3: "salut", | |
} |
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
// https://www.typescriptlang.org/play?ssl=19&ssc=50&pln=1&pc=1#code/C4TwDgpgBAglC8UDeAoK6pgE4HswEYAuKAZ2CwEsA7AczQ2zwCZizLb71GwBmV86nQC+KFKEhQAQgigB5ALYVgAHhgAaKAHJuTTQD5R46AGEZCpao3bcvfaIgAPMDizAoRqACUIJAK4AbN0Q4AB8pKDDjeycXNw8AcQgqABVwCGVkqEdgJIATEi8fAOA9GWTRAGMcKjJYKGJElLTVUsRkbiIoACIQHC6NHWIu4D6Bmz5uqq6RFCqat2kGpNTIZUlW9CQOod7+zHGhqZm52tMlptXjDa2bTp7R-eYhkemgA | |
// Similar to explicit | |
type A = { | |
prop1: string | |
prop2: string | |
prop3: string | |
} | |
type B = Omit<A, 'prop2'> | |
type C = Omit<A, 'prop3'> | |
export type Result = A | B | C | |
export type GenType<T extends Result> = T | |
const A : GenType<A> = {prop1: "yo", prop2: "to", prop3: "co"} | |
const B : GenType<B> = {prop1: "yo", prop3: "co"} | |
const C : GenType<C> = {prop1: "yo", prop2: "to"} |
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
// https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeBYAUFTUwCcD2YAjAFxQDOwOAlgHYDm6W2+YATKRdfY1rgQMwdKtBmgC+6dKEhQAQgigB5ALZVgAHhgAaKAHI+bXQD5JaadADCClWs079LfsdPmoADXUAVIws9QIAB7AEDQAJmSwUAD8kaR+gcFhEfIx8qQWANymAMZ4NBRQ2TgQAIbBbgpe-kEh4VAACiU4wFQlADaaRkYAFABucQCUpB7eCD69UCURI0ZZaOi5+cBQVGRWiEWl5d1IUDyYBiRQAEQgeMda+8wE7CcA7hBkABYX6GIDcwt5BavyG8VlCBuHZ7DC8FhHU7nS5gg6OUjHMjtACuwFe4g+oK+SxWZDg-y2QJBV0OCLO6KYBluxwezwp4IECKRbVR6PeGVBQA | |
type A = { | |
prop1: string | |
prop2: string | |
prop3: string | |
} | |
type B = Omit<A, 'prop2'> | |
type C = Omit<A, 'prop3'> | |
type X<T> = T extends A ? A : T extends B ? B : C; | |
const createX = <T extends Partial<A>>(v: T): X<T> => v as X<T>; | |
const isC = createX({ | |
prop1: "yo", | |
prop2: "wesh", | |
}); | |
const isB = createX({ | |
prop1: "yo", | |
prop3: "salut", | |
}); | |
const isA = createX({ | |
prop1: "yo", | |
prop2: "wesh", | |
prop3: "salut", | |
}); |
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
// https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeBYAUFTUwCcD2YAjAFxQDOwOAlgHYDm6W2+YATKRdfY1rgQMwdKtBmgC+6dKEhQAQgigB5ALZVgAHhgAaKAHI+bXQD5JaadADCClWs079LfsdMBjPDQpQqZOIgAUeABGAFakcAA+UFaRsgCUpEHBXmSwCEZ6Bk5eNFCJUABk+RksrLrZuSEA3OhuHsDJ8v6JYVCR0XLxFUnecmnFBKXlidVotZ7eVk0hLW2tHQkhyVF9DgJltF0jNe6ewBCeTTO9bbF9qBhY3jABp4VQY3gANhAAdI94dAEvBqyxI0zeWQ3ApFB7PN4fL6ZP48TATYF3MGvd6fPDfFiEGHidBAA | |
type A = { | |
prop1: string | |
prop2: string | |
prop3: string | |
} | |
type B = Omit<A, 'prop2'> | |
type C = Omit<A, 'prop3'> | |
const isA = (obj: A | C | B): obj is A => 'prop3' in obj && 'prop2' in obj; | |
const isB = (obj: A | C | B): obj is B => 'prop2' in obj; | |
const isC = (obj: A | C | B): obj is C => 'prop3' in obj; | |
const test = (o: A | B | C) => { | |
isA(o) && console.log(o.prop2); | |
isB(o) && console.log(o.prop3); | |
isC(o) && console.log(o.prop1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment