我有以下代碼接受possibleColumns
給定資料庫表的字串陣列。然后它使用傳入source
并嘗試將其與包含{source}_id
.
例如,possibleColumns
可以包含["id", "name", "duedil_id", "salesforce_id"]
和來源duedil
,然后檢查任何被呼叫的列duedil_id
并回傳它。
邏輯很好,但回傳的型別不正確,目前它只回傳可能的值作為該型別中的任何列,但我想使用模板文字型別只回傳包含<value>_id
. 正如評論中所見,我正在嘗試使用該Extract
型別但沒有運氣。
/**
* Takes a list of columns for a specific table and a source and checks for any columns that
* match `${source}_id`.
* @template T The desired output type
* @param possibleColumns string array of columns of type T
* @param source The source to convert in question
* @returns a key of one of the columns of T or undefined if not matched
*/
export function getProviderIdColumn<T>(possibleColumns: (keyof T)[], source: string) {
// Attempt to extract only keys that contain _id at the end
// as Extract<keyof T extends `${infer _}_id` ? keyof T : never, T>
const providerIdColumn = `${source}_id` as keyof T;
if (possibleColumns.includes(providerIdColumn)) {
return providerIdColumn;
}
電流輸出
"id" | "name" | "duedil_id" | "salesforce_id"
期望的輸出
"duedil_id" | "salesforce_id"
我對打字稿的了解不是很好,所以請忽略任何濫用的術語。
最小的作業示例
export interface Model {
id: number,
name: string | null,
boardex_id: string | null,
source: string,
duedil_id: string | null,
zoom_info_id: string | null,
}
/**
* Takes a list of columns for a specific table and a source and checks for any columns that
* match `${source}_id`.
* @template T The desired output type
* @param possibleColumns string array of columns of type T
* @param source The source to convert in question
* @returns a key of one of the columns of T or undefined if not matched
*/
export function getProviderIdColumn<T>(possibleColumns: (keyof T)[], source: string) {
// Attempt to extract only keys that contain _id at the end
// as Extract<keyof T extends `${infer _}_id` ? keyof T : never, T>
const providerIdColumn = `${source}_id` as keyof T;
if (possibleColumns.includes(providerIdColumn)) {
return providerIdColumn;
}
return undefined;
}
// A function here returns a string array of the keys in Model.
const columnInfo: (keyof Model)[] = ["id", "name", "boardex_id", "source", "duedil_id", "zoom_info_id"];
const source = "boardex";
// Returned values here are fine but I want to get the desired output.
const providerIdColumn = getProviderIdColumn<Model>(columnInfo, source);
游樂場鏈接
uj5u.com熱心網友回復:
我們可以通過使用這個映射型別來提取_id
屬性名稱來做到這一點:
/**
* Gets a union of the literal types in `T` that end with `_id`.
*/
type IdKeys<T extends any[]> = keyof {
[Key in T[number] as Key extends `${string}_id` ? Key : never]: null;
};
然后在函式中使用它:
/**
* Takes a list of columns for a specific table and a source and checks for any columns that
* match `${source}_id`.
* @template ObjectType The desired output type
* @param possibleColumns string array of columns of type T
* @param source The source to convert in question
* @returns a key of one of the columns of T or undefined if not matched
*/
export function getProviderIdColumn<ObjectType>(
possibleColumns: (keyof ObjectType)[],
source: string
): IdKeys<(keyof ObjectType)[]> | undefined {
// Sadly, we have to have a type assertion, since `source` is type `string`
const providerIdColumn = `${source}_id` as IdKeys<(keyof ObjectType)[]>;
if (possibleColumns.includes(providerIdColumn)) {
return providerIdColumn;
}
return undefined;
}
結果是在您的示例呼叫中:
const providerIdColumn = getProviderIdColumn<Model>(columnInfo, source);
...providerIdColumn
的型別是"boardex_id" | "duedil_id" | "zoom_info_id" | undefined
.
游樂場示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517341.html
標籤:打字稿