gpt4 book ai didi

Argument of type 'string' is not assignable to parameter of type 'UUID'(‘字符串’类型的参数不能赋值给‘uuid’类型的参数)

转载 作者:bug小助手 更新时间:2023-10-24 19:27:29 32 4
gpt4 key购买 nike



why I get this error ?

为什么我会收到这个错误?


Argument of type 'string' is not assignable to parameter of type 'UUID'.

I created a class named uuid and then want it to use it with typescript

我创建了一个名为UUID的类,然后希望它与TypeScrip一起使用


UUID.ts

UUID.ts


// Typescript UUID
export class UUID {
private str: string;

constructor(str?: string) {
this.str = str || UUID.getNewGUIDString();
}

toString() {
return this.str;
}

private static getNewGUIDString() {
// your favourite guid generation function could go here
// ex: http://stackoverflow.com/a/8809472/188246
let d = new Date().getTime();
if (window.performance && typeof window.performance.now === "function") {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
let r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
}

server.ts

Server.ts


// type err
const data = await CreateBilling('a1208217-3587-40e1-97ab-695b01cae342');

CREATE BILLING

创建帐单



const CreateBilling = async (shop_id: UUID) => {
try {
// get the categories and remove the same categories
const data = await ShopGetCategories(shop_id);

console.log(data);
return data;
} catch(e) {
console.log(e);
Sentry.captureException(e);
return e;
}
};

export default CreateBilling;

I am thankful for your help I dont know what I am doing wrong how can I solve this issue to use a UUID as type

我很感谢你的帮助我不知道我做错了什么我怎么才能用UUID作为类型来解决这个问题


..........................................................................................................................................................

..........................................................................................................................


更多回答

UUID is a class you need to construct the class with the uuid string. Change await CreateBilling('a1208217-3587-40e1-97ab-695b01cae342') to await CreateBilling(new UUID('a1208217-3587-40e1-97ab-695b01cae342'))

UUID是使用UUID字符串构造类所需的类。将等待CreateBilling(‘a1208217-3587-40e1-97ab-695b01cae342’)更改为等待创建计费(新的uuid(‘a1208217-3587-40e1-97ab-695b01cae342’))

Why do you use custom uuid generator? You can use crypto.randomUUID if you use node >=14 or use uuid npm package or custom generator if you are using node <14.

为什么要使用定制的UUID生成器?如果您使用NODE>=14,则可以使用加密的随机UUID;如果您使用的是NODE<14,则可以使用UUID NPM包或定制生成器。

优秀答案推荐

CreateBilling() accepts an argument shop_id of type UUID which is a class. So you should probably be calling the function as follows and pass the uuid string to the constructor.

CreateBilling()接受uuid类型的参数shop_id,它是一个类。因此,您可能应该如下所示调用该函数,并将UUID字符串传递给构造函数。


const data = await CreateBilling(
new UUID("a1208217-3587-40e1-97ab-695b01cae342")
);

更多回答

32 4 0