英文:
Typescript is there a way to cast type for case in switch?
问题
I have a function that checks request data to access an API endpoint. For each endpoint, there can be different preloaded data for this specific endpoint.
The problem is that for every operation, I need to cast request data to the endpoint data type. Is there a way to assert it only one time for the case block scope? Or should I take a different approach?
[Playground](https://www.typescriptlang.org/play/index.html?ssl=21&ssc=6&pln=1&pc=1#code/FAAjIFwTwBwUxAJTgRwDwBUQF4QFcA7AawIHsB3AgPhxAG8Q4CATGUgSwIgC4QBnCACdOAcwA0IZgEMIU3hgC ocMrDR4IAKIsAjLQHCCI1ZFgJtzAEwB9AMy0CeALYAjOIJMmAxqQICQXgAWcF5EiKQANgi4ABSCqLyEJBQEAJS8LqSRcFIEODR0JuD85OwQQSAxcaggUnxIqKkAdEysHFyp9EXF4F51CADkrWxcOgO8hT1TPewAZlXxKLX1yOgWOlTN0rK0A4HsA53xEHiCeUJ4CCDdU0rTPX18g8O EJbjNw-9IEMsIxC2cZde73apLOoNNYsGy2TZNbZSEAAalwOgA3J9QYtlpC0BYYXCESAAFSojEg6ZgnGrPHQuyEmSIgC0ZMx0zmCxqEJp PpW0ZtAArEc4CczpBBJcwGywHcKZI4LMpHgIjwQMdTnklREnp85bKgA)
type Req<T = unknown> = { endpoint: string, data: T }
type End1 = string
type End2_3 = number
const checkRole = (req: unknown): boolean => {
switch ((req as Req).endpoint) {
case 'endpoint1': {
if ((req as Req
}
case 'endpoint2':
case 'endpoint3': {
(req as Req<End2_3>).data += 1;
(req as Req<End2_3>).data *= 1;
(req as Req<End2_3>).data -= 1;
if ((req as Req<End2_3>).data === 5) return true
}
default: return false
}
}
UPD. [Variant that Jared suggested](https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=27&pc=2#code/MYewdgzgLgBAlhAKjAvDAPIgfACgE4CmAjgFwwCuYA1mCAO5gCUZhR8EMyKWMA3gFAwhMQlHJ4wMKHnIF AX36DhUAJ4AHAjABKxTKgrVaDHml4wCYACbqQcMFDLQ89gOYAaGFYCGUb2URFYRhlITVNGABRawBGA2c3UKkNLWirACYAfQBmAzByAFsAIwI8JKTQSFhgAAsCYCptEAAbLTR8YjJKGnomMiKQFoJvSW4 JOCIOjgoWpgcDrZvDl0iRgA6Sxs7B0Zx4IPhYGWtAHIt2wcY07IBQ-vhOAAzBdYYZZ09NJisDZ8-AynGpwU57UTiSTSWRCCb3IIPI4nGDnayXKDpG6ww7HCBnC7gKDZG77BGHZ7zBCIdCrdBpLLZLC4ViMPZ3UkI1jrf7eGAAajQMQA3Fj2UJOdy QLhaKHuLfDz TAhSLRXKAYrlTL7uCJFIZFoQlqhPDSSaEVYCE9vORmo4RAQxLqrc1cSLFPIgA
英文:
I have a function that check request data to access api endpoint. For each endpoint there can be different preLoaded data for this specific endpoint.
Problem is that for every operation i need to cast request data to endpoint data type. Is there a way to assert it only one time for case block scope? Or i should take some different approach.
type Req<T = unknown> = { endpoint: string, data: T}
type End1 = string
type End2_3 = number
const checkRole = (req: unknown): boolean => {
switch ((req as Req).endpoint) {
case 'endpont1': {
if((req as Req<End1>).data = 'hi') return true
}
case 'endpont2':
case 'endpont3': {
(req as Req<End2_3>).data += 1;
(req as Req<End2_3>).data *= 1;
(req as Req<End2_3>).data -= 1;
if((req as Req<End2_3>).data = 5) return true
}
default: return false
}
}
答案1
得分: 2
根据您的用例具体情况,您可能会对判别联合感兴趣:
type Req = { endpoint: "endpoint1", data: string }
| { endpoint: "endpoint3" | "endpoint2", data: number }
const checkRole = (_req: unknown): boolean => {
let req = _req as Req;
switch (req.endpoint) {
case 'endpoint1': {
if (req.data === 'hi') return true;
return false;
}
case 'endpoint3':
case 'endpoint2': {
req.data += 1;
req.data *= 1;
req.data -= 1;
if (req.data === 5) return true;
}
default: return false;
}
}
英文:
Depeding on the specifics of your use case, you might be interested in discriminated unions:
type Req = { endpoint: "endpoint1", data: string }
| { endpoint: "endpoint3" | "endpoint2", data: number }
const checkRole = (_req: unknown): boolean => {
let req = _req as Req;
switch (req.endpoint) {
case 'endpoint1': {
if (req.data = 'hi') return true
return false;
}
case 'endpoint3':
case 'endpoint2': {
req.data += 1;
req.data *= 1;
req.data -= 1;
if (req.data == 5) return true
}
default: return false
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论