我正在使用visual studio代码进行打字稿项目,在那里我使用了一些第三方npm js库.其中一些不提供任何ts类型(types.d.ts文件),因此每当我使用参数或变量而不指定其类型时,vs代码的linting显示此错误:参数隐式具有"any"类型. 此外,ts不会编译.
我怎样才能防止这种情况发生?
那么有谁知道为什么我在使用 mongoose hook 和引用this. 我没有使用箭头函数,并且我知道它的词法作用域问题,但即使使用这样的匿名函数:
UserSchema.pre("save", function(next) {
console.log(this.password);
next();
});
Run Code Online (Sandbox Code Playgroud)
确切的错误消息是
'this' implicitly has type 'any' because it does not have a type annotation.
Run Code Online (Sandbox Code Playgroud)
有人知道如何解决这个问题吗?
顺便说一句,我正在使用 Typescript 2.5.2 / NodeJS 8.2.1
谢谢!
假设我有一个带有如下数据的 vue 组件:
data: () => ({
form: {
old_password: {
data: '',
type: 'password',
label: 'Old Password',
},
new_password: {
data: '',
type: 'password',
label: 'New Password',
},
repeat_password: {
data: '',
type: 'password',
label: 'New Password Confirmation',
},
},
}),
Run Code Online (Sandbox Code Playgroud)
数据以这种方式格式化,因为我使用另一个插件 ant-design 来构建表单,因此以另一种方式格式化数据不是一种选择。该data字段将存储实际数据。
然后,我为 vuelidate 设置了以下验证规则。
validations: {
form: {
old_password: {
data: { required },
},
new_password: {
data: { required },
},
repeat_password: {
data: { sameAsPassword: sameAs('new_password') },
},
},
},
Run Code Online (Sandbox Code Playgroud)
该required …
最近,当我使用Rxjs 5时,我通过使用npm install Rxjs@5.0.1从node_modules下的下载代码中下载了Rxjs,在Rxjs文件夹中找到了Observable.d.ts,我看到它声明了它的构造函数,如下所示:
*
* @constructor
* @param {Function} subscribe the function that is called when the Observable is
* initially subscribed to. This function is given a Subscriber, to which new values
* can be `next`ed, or an `error` method can be called to raise an error, or
* `complete` can be called to notify of a successful completion.
*/
constructor(subscribe?: <R>(this: Observable<T>, subscriber: Subscriber<R>) => TeardownLogic);
Run Code Online (Sandbox Code Playgroud)
我的问题是:在subscribe的函数类型声明中此关键字的用法是什么?:(此:Observable,...),TypeScript是否有一些有关此关键字用法的文档?谢谢。