最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Merging types in typescript (adding keys to an existing keyof type) - Stack Overflow

programmeradmin10浏览0评论

If I have a typescript type consisting of keys:

const anObject = {value1: '1', value2: '2', value3: '3'}
type objectKeys = keyof typeof anObject

and then I wish to add keys to that type, while retaining the current keys, how do I go about doing that?
for example, if I wanted to add the keys 'get_value1', 'get_value2', 'get_value3' to the type 'objectKeys'

In the end, I want a type that looks like so:

type objectKeys = keyof anObject + 'get_value1', 'get_value2', 'get_value3'

without having to manually define the keys prefixed with 'get_', I understand that I can type out the keys to create this object - however that is not feasible for my use case. I simply want to add some keys that may or may not exist to the type 'objectKeys'

I am also aware that I can create a generic or any type that allows for any key value, however I must know the actual key names. It does not help me to allow for ANY key to be requested of the object, I need the existing keys + the ones I'd like to add.

Thanks for any help.

added for clarity:

const anObject = {val1: '1', val2: '2'}
type objectKeys = keyof typeof anObject

Object.keys(anObject).forEach(key => {
  const getAddition = `get_${key}`
  anObject[getAddition] = getAddition
})

// now I don't know whats next, how do I update objectKeys to include the 
// additions added in the forEach loop.

// What I really want is to not have to add the 'get' values to the object 
// at all, JUST to the type.  I want typechecking for the get values that 
// may or may not actually exist on the object.

hope thats clearerer and such.

If I have a typescript type consisting of keys:

const anObject = {value1: '1', value2: '2', value3: '3'}
type objectKeys = keyof typeof anObject

and then I wish to add keys to that type, while retaining the current keys, how do I go about doing that?
for example, if I wanted to add the keys 'get_value1', 'get_value2', 'get_value3' to the type 'objectKeys'

In the end, I want a type that looks like so:

type objectKeys = keyof anObject + 'get_value1', 'get_value2', 'get_value3'

without having to manually define the keys prefixed with 'get_', I understand that I can type out the keys to create this object - however that is not feasible for my use case. I simply want to add some keys that may or may not exist to the type 'objectKeys'

I am also aware that I can create a generic or any type that allows for any key value, however I must know the actual key names. It does not help me to allow for ANY key to be requested of the object, I need the existing keys + the ones I'd like to add.

Thanks for any help.

added for clarity:

const anObject = {val1: '1', val2: '2'}
type objectKeys = keyof typeof anObject

Object.keys(anObject).forEach(key => {
  const getAddition = `get_${key}`
  anObject[getAddition] = getAddition
})

// now I don't know whats next, how do I update objectKeys to include the 
// additions added in the forEach loop.

// What I really want is to not have to add the 'get' values to the object 
// at all, JUST to the type.  I want typechecking for the get values that 
// may or may not actually exist on the object.

hope thats clearerer and such.

Share Improve this question edited Aug 31, 2017 at 5:45 laramie asked Aug 31, 2017 at 4:20 laramielaramie 771 silver badge6 bronze badges 1
  • 1 As far as I read the specification for keyof and mapped types, there is no way to achieve this. Hope I'm wrong, but transforming key names is not a feature I've ever seen the typescript team demo. – Titian Cernicova-Dragomir Commented Aug 31, 2017 at 5:15
Add a ment  | 

3 Answers 3

Reset to default 3

It sounds like you're asking for concatenation of string literal types: that is, you want to be able to take the string literal "get_" and another string literal like "value1", and have TypeScript understand that if you concatenate strings of those types you get a string of the type "get_value1". Unfortunately, this feature does not exist as of TypeScript 2.4 (and probably won't exist in 2.5 or 2.6 either)

发布评论

评论列表(0)

  1. 暂无评论