Created
April 14, 2020 17:39
-
-
Save ianmstew/1e718f63c318f2d67318db2a3099c068 to your computer and use it in GitHub Desktop.
record type with exceptional properties
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 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