[英]Property 'host' does not exist on type 'ProxyTargetUrl' Error. Even though i get IntelliSense in JS File
我在.ts
文件中有以下代碼:
const connection = {
host: options.target.host
};
options
的類型為httpProxy.ServerOptions
, target
的類型為ProxyTargetUrl
我得到的錯誤如下:
Property 'host' does not exist on type 'ProxyTargetUrl'.
Property 'host' does not exist on type 'string'.
這些選項是http-proxy
npm模塊的一部分。 最新的類型已安裝。 "@types/http-proxy": "^1.16.2"
當我遵循聲明時,會看到以下內容:
type ProxyTargetUrl = string | url.Url;
-
interface Url extends UrlObjectCommon {
port?: string;
query?: string | null | ParsedUrlQuery;
}
-
interface UrlObjectCommon {
auth?: string;
hash?: string;
host?: string;
hostname?: string;
href?: string;
path?: string;
pathname?: string;
protocol?: string;
search?: string;
slashes?: boolean;
}
為什么會出現此錯誤? 當我將代碼復制到JS文件中時,我獲得了IntelliSense的host
屬性,並且代碼在運行時也運行良好。 因此鍵入是正確的。 我錯過了什么?
問題是在您的聯合類型中, type ProxyTargetUrl = string | url.Url
type ProxyTargetUrl = string | url.Url
,只有Url
定義了字段host
。
要訪問此字段,您需要類型斷言: (<Url>options.target).host
。
有關更多信息,請參見此Github問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.