我有這個列舉
enum MyTypes {
TypeA = 'TypeA';
TypeB = 'TypeB';
}
用那個列舉,我建立了這個記錄:
const MyRecord: Readonly<Record<MyTypes, string>> = {
[MyTypes.TypeA]: 'end-point-a',
[MyTypes.TypeB]: 'end-point-b'
} as const
現在,我想構建一個回傳的函式,'end-point-a'
或者'end-point-b'
我可以在開關上使用回傳的值,例如
const myEndpoint = getEndpoint();
switch(myEndpoint){
case MyRecord.MyTypeA:
///
break;
case MyRecord.MyTypeB:
///
break;
default:
thisFailsWithOtherStuffThanNever(myEndpoint) // This should not fail
}
但我不知道如何定義“MyRecord 中的值”。這甚至可能嗎?
操場
uj5u.com熱心網友回復:
不允許您使用Readonly<...>
帶有as const
斷言的顯式型別。你需要satisfies
改用。這是TS 4.9中引入的新關鍵字
const MyRecord = {
[MyTypes.TypeA]: 'end-point-a',
[MyTypes.TypeB]: 'end-point-b'
} as const satisfies Readonly<Record<MyTypes, string>>
現在,您有一個滿足您的型別的保證,MyRecord
并且您有型別推斷。
現在,您需要創建一個包含MyRecord
obj 值的型別。讓我們命名它Endpoints
:
enum MyTypes {
TypeA = 'TypeA',
TypeB = 'TypeB',
}
const MyRecord = {
[MyTypes.TypeA]: 'end-point-a',
[MyTypes.TypeB]: 'end-point-b'
} as const satisfies Readonly<Record<MyTypes, string>>
type Values<T> = T[keyof T]
type EndpointMap = typeof MyRecord
type Endpoints = Values<EndpointMap>
整個代碼:
enum MyTypes {
TypeA = 'TypeA',
TypeB = 'TypeB',
}
const MyRecord = {
[MyTypes.TypeA]: 'end-point-a',
[MyTypes.TypeB]: 'end-point-b'
} as const satisfies Readonly<Record<MyTypes, string>>
type Values<T> = T[keyof T]
type EndpointMap = typeof MyRecord
type Endpoints = Values<EndpointMap>
function thisFailsWithOtherStuffThanNever(nope: never) {
}
function getEndpoint(): Endpoints {
return MyRecord.TypeA;
}
const myEndpoint = getEndpoint();
const foo = (endpoint: Endpoints) => {
switch (endpoint) {
case MyRecord.TypeA:
///
break;
case MyRecord.TypeB:
///
break;
default:
thisFailsWithOtherStuffThanNever(endpoint)
}
}
操場
uj5u.com熱心網友回復:
您已經在兩個地方定義了映射登錄:
第一名是 MyRecord
const MyRecord: Readonly<Record<MyTypes, string>> = {
[MyTypes.TypeA]: 'end-point-a',
[MyTypes.TypeB]: 'end-point-b'
} as const
第二個在 swithc/case 陳述句中。
const myEndpoint = getEndpoint();
switch(myEndpoint){
///..
}
你必須選擇一個。我將提出兩種解決方案。我更喜歡第一個。提供游樂場鏈接
解決方案一:使用該映射物件。這里的游樂場
const endpointMapping: Readonly<Record<string, string>> = {
[MyTypes.TypeA]: 'end-point-a',
[MyTypes.TypeB]: 'end-point-b'
} as const
const myEndpoint = getEndpoint();
const mappedEndpoingValue = endpointMapping[myEndpoint];
if (mappedEndpoingValue !== undefined) {
console.log(`I have foudn the mapping it is ${mappedEndpoingValue}`)
} else {
// Handle it. Probably throw an error
console.error(`Not supportend endpoint type ${myEndpoint}`);
}
解決方案二:使用 switch/case 陳述句。Playgroudn 使用開關盒
const myEndpoint = getEndpoint();
let mappedEndpoingValue: string = "";
switch (myEndpoint) {
case MyTypes.TypeA:
mappedEndpoingValue = "end-point-a"
break;
case MyTypes.TypeB:
mappedEndpoingValue = "end-point-a"
break;
default:
// Handle it. Probably throw an error
console.error(`Not supportend endpoint type ${myEndpoint}`);
throw new Error("not fount mapping")
}
console.log(`I have foudn the mapping it is ${mappedEndpoingValue}`)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516224.html
標籤:打字稿