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 badges1 Answer
Reset to default 1The 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
}