Created
April 1, 2021 07:32
-
-
Save marcolink/b18982dc22da96a376e0e44dbb20e5ae to your computer and use it in GitHub Desktop.
Helper for localized (contentful) Entry type
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://github.com/contentful/contentful.js/blob/master/index.d.ts#L81 | |
*/ | |
type Entry<Fields> = { | |
sys: { | |
id: string | |
} | |
fields : Fields | |
} | |
type LocalizedFields<Fields, Locales extends keyof any> = { | |
[FieldName in keyof Fields]?: { | |
[LocaleName in Locales]?: Fields[FieldName]; | |
} | |
} | |
type LocalizedEntry<EntryType, Locales extends keyof any> = { | |
[Key in keyof EntryType] : | |
Key extends 'fields' | |
? LocalizedFields<EntryType[Key], Locales> | |
: EntryType[Key] | |
} | |
type MyFields = { | |
a: string | |
b: number | |
} | |
type MyLocalizedEntry = LocalizedEntry<Entry<MyFields>, 'en-US' | 'de-DE'>; | |
const myEntry: Entry<MyFields> = { | |
sys: { | |
id: "dfsdf" | |
}, | |
fields: { | |
a: "hello", | |
b: 1 | |
} | |
} | |
const localizedEntry: MyLocalizedEntry = { | |
sys : { | |
id: 'my-id' | |
}, | |
fields: { | |
a: { | |
"de-DE": 'german translation', | |
"en-US": 'englisch translation' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.typescriptlang.org/play?#code/PTAEAsBdIBwZwFwgOYEtLgK4CMB0BjAewFtgiA7SAU0oDNMAbMwymyeh3AKzmGwcLZgxAIZxqAJ2CpyAEyoAPXLNyQ4AYgAyADgCMAKEgBPGFVABRShKMAeAGKoqDWXAB8oALygA3vtCg4I0QfP39QVFkEAMgJGWRQgF9Q2kdnOFAohycXfSTDEzNNQnwRBlQALypZLLT7VJcAGlAikoYqdMVqOXSAayojQlpQEXIjd08QsNAAbRrZADkRYjMZUD6Bobm4AF0AfijfKf9pltKqReXw8mbis5390C3Z+ouqbYBuUP8kvONTG9aFSqlhithB1gAKgUmqc2h0FF0XGt+oNhqNxl5DtMANL9K7IjYWKxGKGmbYZUChXFGUCdGhIgDkKWycAZlP8uwBpSB1XqcBs4JJBRx-W2MNucNcoSigtJVBFRm2uX0+X+AFkjFsJodhlFxLFyPF-NgouRMMRsFQJMrVWYNbCeYKJg7KrJBQLiTYNVtXE0GTQALQAVQAymyAD6gBnyAMAEXMDNcn30FHEoGIRkFMs93r57kxoUCwR1-giUQARLJaHAq+XEg1knyDl9daBy+AnAJyw2pibQAZvjbU5B00YXcDiVF7RLHcTtYWghSS+FIlGMwGIgz642Wc2piI90dK1Q4+Zy1EGcgraJrjERnAGCJIKgWAye0fA6Hz1GaMgynB8HAUA73IB8nxfcg2RbJJBySIA