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

typescript - "Pick" a specific implementation of a type which is anonymous - Stack Overflow

programmeradmin0浏览0评论

Here is an example of some types and a function:

type testM3 = { a: 'gggg', other: 'fggfg' }
type totalM = { a: '123', result: { something: '4444' }}| { a: '321' } | testM3 | null

// basically I want something like : totalM['a' === '123']
function funcc2(input: totalM): ? {  <-- this is what I have no idea how to specify return type
  if(input && 'result' in input){ // maybe this somehow?
    return input
  }
  if(input && input.a === '321'){ // or this?
    return input
  }
  return null
}

I want to specify a specific return type. In this case the type I want to return is "anonymous" the one which contains "result" in it. Since it is not a specifically named type, I cannot just say something like : testM3. I could just say that the return is : { a: '123', result: '4444' }. But that would only work for a small type. If the example return would contain 50 different sub properties, that would be unfeasible. I also have no control over the totalM type, so I can not just change the type itself. Is there an easy way to "pick" the implementation with something like totalM['a' === '123']? This is not about picking a specific return value, but to return the whole return object. So, something like Pick<> I assume will not work.

I could not find anything that would fit from the TypeScript documentation. I assume this would be called discriminated union, but some suggestions would be, either change the total type itself (which I can not do) or have a custom implementation, which can be used, but most solutions are several years old. So I am interested if there is maybe a easy and fast solution for it or not.

Here is an example of some types and a function:

type testM3 = { a: 'gggg', other: 'fggfg' }
type totalM = { a: '123', result: { something: '4444' }}| { a: '321' } | testM3 | null

// basically I want something like : totalM['a' === '123']
function funcc2(input: totalM): ? {  <-- this is what I have no idea how to specify return type
  if(input && 'result' in input){ // maybe this somehow?
    return input
  }
  if(input && input.a === '321'){ // or this?
    return input
  }
  return null
}

I want to specify a specific return type. In this case the type I want to return is "anonymous" the one which contains "result" in it. Since it is not a specifically named type, I cannot just say something like : testM3. I could just say that the return is : { a: '123', result: '4444' }. But that would only work for a small type. If the example return would contain 50 different sub properties, that would be unfeasible. I also have no control over the totalM type, so I can not just change the type itself. Is there an easy way to "pick" the implementation with something like totalM['a' === '123']? This is not about picking a specific return value, but to return the whole return object. So, something like Pick<> I assume will not work.

I could not find anything that would fit from the TypeScript documentation. I assume this would be called discriminated union, but some suggestions would be, either change the total type itself (which I can not do) or have a custom implementation, which can be used, but most solutions are several years old. So I am interested if there is maybe a easy and fast solution for it or not.

Share Improve this question edited Mar 12 at 14:26 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 12 at 14:24 KaboomyKaboomy 231 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The second narrowing is ok, just remember to fix to '123'. You don't need t bother with the return type, TS infers it correctly. Also make it a separate type for reusing:

Playground

type testM3 = { a: 'gggg', other: 'fggfg' }
type testM1 = { a: '123', result: { something: '4444' }};
type totalM = testM1 | { a: '321' } | testM3 | null

function funcc2(input: totalM){
  if(input?.a === '123'){
    return input
  }
  return null
}
发布评论

评论列表(0)

  1. 暂无评论