Skip to content

Instantly share code, notes, and snippets.

@ianmstew
Created April 14, 2020 17:39
Show Gist options
  • Save ianmstew/1e718f63c318f2d67318db2a3099c068 to your computer and use it in GitHub Desktop.
Save ianmstew/1e718f63c318f2d67318db2a3099c068 to your computer and use it in GitHub Desktop.
record type with exceptional properties
// Type and Factory
type RecordX = ReturnType<typeof RecordX>;
function RecordX(
id: string,
props?: Record<string, string[]>
) {
return Object.assign({ id }, props);
}
// USAGE
const model: RecordX = RecordX('id-1', {
foo: ['a'],
})
// type string
const id = model.id;
// type string[]
const otherProp = model.otherProp;
// string is not assignable to string[]
const model2: RecordX = RecordX('id-2', {
bar: 'hello'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment