Last active
May 3, 2019 12:45
-
-
Save alexandcote/e0b4d75bf90dcf50458525f55652726a to your computer and use it in GitHub Desktop.
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
// Global payload | |
interface Global { | |
myGlobal: string; | |
myOtherGlobal: string; | |
} | |
// Type with all global keys | |
interface Schema1 { | |
schemaId: 'schema1'; | |
payload: { | |
myGlobal: string | |
myOtherGlobal: string; | |
custom1: string; | |
}; | |
} | |
// Type with only one global key | |
interface Schema2 { | |
schemaId: 'schema2'; | |
payload: { | |
myGlobal: string | |
custom2: string; | |
} | |
} | |
// Type with the other global key | |
interface Schema3 { | |
schemaId: 'schema3'; | |
payload: { | |
myOtherGlobal: string | |
custom3: string; | |
} | |
} | |
// Type without global key | |
interface Schema4 { | |
schemaId: 'schema4'; | |
payload: { | |
custom4: string; | |
} | |
} | |
// Union type (I can't really change this) | |
type Schema = Schema1 | Schema2 | Schema3 | Schema4; | |
// New union type without global key in the payload | |
type NewUnionTypeWithoutGlobal = ??? | |
// Basic usage without any global key in the payload | |
const schemaWithoutGlobal: NewUnionTypeWithoutGlobal = { | |
schemaId: 'schema1', | |
payload: { | |
custom1: string; | |
} | |
} | |
// Usage when we can redefined some global keys if we want | |
const schemaWithOverriteSomeGlobals: NewUnionTypeWithoutGlobal = { | |
schemaId: 'schema1', | |
payload: { | |
myGlobal: string; | |
custom1: string; | |
} | |
} | |
// Should not work because the schemaId doesn't match the payload. | |
const schemaWithOverriteSomeGlobals: NewUnionTypeWithoutGlobal = { | |
schemaId: 'schema2', | |
payload: { | |
// Payload need to be grouped with the good schemaID | |
custom1: string; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment