【发布时间】:2021-04-25 17:14:48
【问题描述】:
我有一个函数URLBuilder,它作为一个模块工作(它返回一个包含各种函数的对象,并且这些函数与上下文对象很好地绑定,所以this 绑定按预期工作,我们可以访问@ 987654323@里面这些函数正确)
我的问题是为什么打字稿不理解URLBuilder 的返回对象内的函数this 绑定?即使我尝试手动添加返回类型,Typescript 也会抱怨 when using the URLBuilder 在没有 this 上下文的情况下无法接受该函数。
我们如何让 typescript 理解返回对象中这个绑定函数的this?
我要解决的 Typescript 错误是:
The 'this' context of type 'URLBuilderAPI' is not assignable to method's 'this' of type 'ExecutionContext'.
interface URLBuilderOptions {
baseURL: string,
}
interface ExecutionContext {
options: URLBuilderOptions,
predefinedValues: { protocol: string },
}
/************************* */
function generateRequestURL(this: ExecutionContext, customOptions: { subRoute: string }) {
return `${this.predefinedValues.protocol}:${this.options.baseURL}/api/v3/${customOptions.subRoute}/some-url`;
}
/************************* */
function URLBuilder(options: URLBuilderOptions) {
const predefinedValues = {
protocol: 'https',
}
const context = {
options,
predefinedValues,
}
const API = {
generateRequestURL: generateRequestURL.bind(context)
}
return API;
}
/*********************** */
// 1st try
let urlInstance = URLBuilder({ baseURL: 'www.url.com' });
urlInstance.generateRequestURL({});
// typescript cannot understand function params and auto suggest them!
/*********************** */
interface URLBuilderAPI {
generateRequestURL: typeof generateRequestURL,
}
function URLBuilder_WithReturnTypes(options: URLBuilderOptions): URLBuilderAPI {
const predefinedValues = {
protocol: 'https',
}
const context = {
options,
predefinedValues,
}
const API = {
generateRequestURL: generateRequestURL.bind(context)
}
return API;
}
/*********************** */
// 2nd try
let urlInstanceWithTypes = URLBuilder_WithReturnTypes({ baseURL: 'www.url.com' });
urlInstanceWithTypes.generateRequestURL({ subRoute: '/test/' })
/**
* typescript error:
* The 'this' context of type 'URLBuilderAPI' is not assignable to method's 'this' of type 'ExecutionContext'.
*/
/*********************** */
【问题讨论】:
-
第一个版本有什么问题?我在
generateRequestURL中看到了建议。你用strictBindCallApply吗?因为如果不是,则 bind 将返回any -
我没有用
strictBindCallApply,我现在测试一下
标签: javascript typescript binding this