转换为Typescript时如何键入“this”
我在一个带有传单的Typescript Vue项目中工作。我发现了一些延迟加载映射标记的代码,但它是用Javascript编写的。它工作得很好,但是我收到了VSCode错误和编译器警告,因为转换为Typescript时如何键入“this”,typescript,vue.js,Typescript,Vue.js,我在一个带有传单的Typescript Vue项目中工作。我发现了一些延迟加载映射标记的代码,但它是用Javascript编写的。它工作得很好,但是我收到了VSCode错误和编译器警告,因为这个没有被键入。如何键入此 更新1:我使用下面的@aplet123建议获得了要解决的第一个类型。但是我想不出这上面的第二个。我认为这是因为我实际上是在使用该函数来扩展现有的功能(即,\u updateConVisibility实际上并不存在于类型上)。那现在我该怎么办?我怀疑用我需要的方法创建一个自定义类是最
这个
没有被键入。如何键入此
更新1:我使用下面的@aplet123建议获得了要解决的第一个类型。但是我想不出这上面的第二个。我认为这是因为我实际上是在使用该函数来扩展现有的功能(即,\u updateConVisibility
实际上并不存在于类型上)。那现在我该怎么办?我怀疑用我需要的方法创建一个自定义类是最好的做法,但我不知道提供一个匿名对象或其他东西是否更常见
L.Marker.addInitHook(function (this: L.Marker) {
this.on(
'add',
function () {
this._updateIconVisibility = function () {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent
// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode
}
// add/remove from DOM on change
if (isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon)
if (shadow) {
shadowParent.appendChild(shadow)
}
} else {
iconParent.removeChild(icon)
if (shadow) {
shadowParent.removeChild(shadow)
}
}
this._wasVisible = isVisible
}
}
// on map size change, remove/add icon from/to DOM
this._map.on(
'resize moveend zoomend',
this._updateIconVisibility,
this
)
this._updateIconVisibility()
},
this
)
})
我对您的环境(Vue等)不是非常熟悉,但我能够构建以下文件,以消除错误,并希望展示前进的方向。如果我知道的更多,情况可能会有所改善 关键是真正描述你的
每一步的类型。因此,我创建了一点脚手架,创建了一个类型MyObject
来描述您正在实现的装饰,然后在适当的位置声明this
为类型MyObject
请看下面,如果有不清楚的地方,请告诉我
// Scaffolding to account for things from the environment
namespace L {
type handler = () => void
type target = object;
export type on = (types: string, handler: handler, target: target) => void
export interface Marker {
on: on
}
}
const L = {
Marker: {
addInitHook: function(f: (this: L.Marker) => void) {
// just here because your code must be supplying it
}
}
}
// A type to add to your code
namespace L {
export type MyObject = {
_updateIconVisibility: () => void,
_map: {
on: on
}
}
}
// Your code above, now with no objection from the TypeScript compiler
L.Marker.addInitHook(function (this: L.Marker) {
this.on(
'add',
function (this: L.MyObject) {
this._updateIconVisibility = function () {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent
// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode
}
// add/remove from DOM on change
if (isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon)
if (shadow) {
shadowParent.appendChild(shadow)
}
} else {
iconParent.removeChild(icon)
if (shadow) {
shadowParent.removeChild(shadow)
}
}
this._wasVisible = isVisible
}
}
// on map size change, remove/add icon from/to DOM
this._map.on(
'resize moveend zoomend',
this._updateIconVisibility,
this
)
this._updateIconVisibility()
},
this
)
})
我对您的环境(Vue等)不是非常熟悉,但我能够构建以下文件来消除错误,并希望能够演示前进的方向。如果我知道的更多,情况可能会有所改善
关键是真正描述你的每一步的类型。因此,我创建了一点脚手架,创建了一个类型MyObject
来描述您正在实现的装饰,然后在适当的位置声明this
为类型MyObject
请看下面,如果有不清楚的地方,请告诉我
// Scaffolding to account for things from the environment
namespace L {
type handler = () => void
type target = object;
export type on = (types: string, handler: handler, target: target) => void
export interface Marker {
on: on
}
}
const L = {
Marker: {
addInitHook: function(f: (this: L.Marker) => void) {
// just here because your code must be supplying it
}
}
}
// A type to add to your code
namespace L {
export type MyObject = {
_updateIconVisibility: () => void,
_map: {
on: on
}
}
}
// Your code above, now with no objection from the TypeScript compiler
L.Marker.addInitHook(function (this: L.Marker) {
this.on(
'add',
function (this: L.MyObject) {
this._updateIconVisibility = function () {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent
// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode
}
// add/remove from DOM on change
if (isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon)
if (shadow) {
shadowParent.appendChild(shadow)
}
} else {
iconParent.removeChild(icon)
if (shadow) {
shadowParent.removeChild(shadow)
}
}
this._wasVisible = isVisible
}
}
// on map size change, remove/add icon from/to DOM
this._map.on(
'resize moveend zoomend',
this._updateIconVisibility,
this
)
this._updateIconVisibility()
},
this
)
})
David的回答让我定义了一个包含扩展功能成员的接口。结果是这样的:
interface LazyMarker extends Marker {
_wasVisible: boolean
_icon: ((Node & ParentNode) | null) & Icon
_iconParent: (Node & ParentNode) | null
_shadowParent: (Node & ParentNode) | null
_updateIconVisibility(): void
}
// this hook makes the markers lazy load
// increases performance of map scrolling/moving
L.Marker.addInitHook(function (this: LazyMarker) {
this.on(
'add',
function (this: LazyMarker) {
this._updateIconVisibility = function (this: LazyMarker) {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent
// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode
}
// add/remove from DOM on change
if (iconParent != null && isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon)
if (shadowParent != null && shadow) {
shadowParent.appendChild(shadow)
}
} else {
iconParent.removeChild(icon)
if (shadowParent != null && shadow) {
shadowParent.removeChild(shadow)
}
}
this._wasVisible = isVisible
}
}
// on map size change, remove/add icon from/to DOM
this._map.on(
'resize moveend zoomend',
this._updateIconVisibility,
this
)
this._updateIconVisibility()
},
this
)
})
David的回答让我定义了一个包含扩展功能成员的接口。结果是这样的:
interface LazyMarker extends Marker {
_wasVisible: boolean
_icon: ((Node & ParentNode) | null) & Icon
_iconParent: (Node & ParentNode) | null
_shadowParent: (Node & ParentNode) | null
_updateIconVisibility(): void
}
// this hook makes the markers lazy load
// increases performance of map scrolling/moving
L.Marker.addInitHook(function (this: LazyMarker) {
this.on(
'add',
function (this: LazyMarker) {
this._updateIconVisibility = function (this: LazyMarker) {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent
// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode
}
// add/remove from DOM on change
if (iconParent != null && isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon)
if (shadowParent != null && shadow) {
shadowParent.appendChild(shadow)
}
} else {
iconParent.removeChild(icon)
if (shadowParent != null && shadow) {
shadowParent.removeChild(shadow)
}
}
this._wasVisible = isVisible
}
}
// on map size change, remove/add icon from/to DOM
this._map.on(
'resize moveend zoomend',
this._updateIconVisibility,
this
)
this._updateIconVisibility()
},
this
)
})
为函数指定一个名为this
的类型化参数(它不是一个实际参数,不会被编译成结果javascript,只是标记this
类型)。这与您的项目配置有关,请更新tsconfig.json并设置strict:true
@Aplet123谢谢,这对第一个实例有效。我已经更新了我的问题。此外,如果我以后遇到函数需要参数的情况,这仍然有效吗?我能在这个
参数之后传递这些参数吗?它们的顺序正确吗?@BooklynDadCore谢谢,我已经在我的.t中设置了strict:true
,为函数配置一个名为这个
(它不是一个实际的参数,也不会被编译成最终的javascript,它只是表示this
类型)。这与您的项目配置有关。更新您的tsconfig.json并设置strict:true
@Aplet123谢谢这对第一个实例起作用。我已经更新了我的问题。此外,如果我以后遇到函数需要参数的情况,这是否仍然有效?我会在之后传递这些参数吗这个
参数,它们的顺序是否正确?@BooklynDadCore谢谢,我已经在我的.tsconfig中设置了strict:true