gpt4 book ai didi

typescript - 类型 'string | AddressInfo' 没有属性 'port' 也没有字符串索引签名

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:46 24 4
gpt4 key购买 nike

{ port } 中我得到了错误:Type 'string | AddressInfo' 没有属性 'port' 也没有字符串索引签名。

如何解决?

代码:

import * as express from 'express'
const app = express()

app.listen({ port: process.env.PORT })

const { port } = app.address()

我的 tsconfig.json

{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",

"composite": true,
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": ".."
},
"exclude": ["node_modules"],
"include": ["./src/**/*.tsx", "./src/**/*.ts"],
"references": [{ "path": "../common" }]
}

最佳答案

找到解决方案,对我有用:

import * as express from 'express'
import { AddressInfo } from 'net'

const app = express()

app.listen({ port: process.env.PORT })

const { port } = app.address() as AddressInfo

关于typescript - 类型 'string | AddressInfo' 没有属性 'port' 也没有字符串索引签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53736253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com