Skip to content

Instantly share code, notes, and snippets.

@alexandcote
Last active May 3, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandcote/e0b4d75bf90dcf50458525f55652726a to your computer and use it in GitHub Desktop.
Save alexandcote/e0b4d75bf90dcf50458525f55652726a to your computer and use it in GitHub Desktop.
// 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