小学网站建设教程,做推广哪个平台效果好,南京专业网站制作公司有哪些,提供南昌网站建设公司解决字面量类型推断错误的四种方式 方式一#xff1a;对象属性使用类型断言方式二#xff1a;传参使用类型断言方式三#xff1a;对象使用类型断言方式四#xff1a;对象属性使用变量#xff0c;变量使用字面量类型参考 declare function handleRequest(url: string, meth… 解决字面量类型推断错误的四种方式 方式一对象属性使用类型断言方式二传参使用类型断言方式三对象使用类型断言方式四对象属性使用变量变量使用字面量类型参考 declare function handleRequest(url: string, method: GET | POST): void;const req { url: https://www.baidu.com, method: GET }handleRequest(req.url, req.method);报错如下
方式一对象属性使用类型断言
declare function handleRequest(url: string, method: GET | POST): void;const req { url: https://www.baidu.com, method: GET as GET };handleRequest(req.url, req.method);方式二传参使用类型断言
declare function handleRequest(url: string, method: GET | POST): void;const req { url: https://www.baidu.com, method: GET };handleRequest(req.url, req.method as GET);方式三对象使用类型断言
declare function handleRequest(url: string, method: GET | POST): void;const req { url: https://www.baidu.com, method: GET } as const;handleRequest(req.url, req.method);方式四对象属性使用变量变量使用字面量类型
declare function handleRequest(url: string, method: GET | POST): void;const method: GET | POST GET;const req { url: https://www.baidu.com, method };handleRequest(req.url, req.method);参考
literal-inference unit-types