You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express服务器TypeScript报错:'address'/'port'不存在于指定类型

解决Express server.address()的TypeScript类型错误(ts(2339))

问题背景

你在将Express项目从JavaScript迁移到TypeScript时,遇到了两个ts(2339)类型错误:

  • 属性'address'不存在于类型'string | AddressInfo',不存在于类型'string'
  • 属性'port'不存在于类型'string | AddressInfo',不存在于类型'string'

你的原始代码如下:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json())
const db = require('./app/config/db.config.js');
// force: true will drop the table if it already exists
db.sequelize.sync({force: true}).then(() => {
  console.log('Drop and Resync with { force: true }');
});
require('./app/route/data.route.js')(app);
// Create a Server
var server = app.listen(8081, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("App listening at http://%s:%s", host, port)
})

问题原因

Express的server.address()方法返回值的类型是string | AddressInfo:

  • 当服务器监听TCP端口时,返回AddressInfo对象(包含address和port属性)
  • 当服务器监听UNIX套接字时,返回字符串类型的套接字路径

TypeScript无法在编译期确定返回值的具体类型,所以直接访问address和port会触发类型检查错误。

解决方案

方案1:类型断言(适合确定监听TCP端口的场景)

如果你明确服务器是监听TCP端口而非UNIX套接字,可以直接用类型断言告诉TypeScript返回值是AddressInfo类型:

import express from 'express';
import bodyParser from 'body-parser';
import { AddressInfo } from 'net'; // 导入AddressInfo类型
import db from './app/config/db.config.js';

const app = express();
app.use(bodyParser.json());

db.sequelize.sync({ force: true }).then(() => {
  console.log('Drop and Resync with { force: true }');
});

require('./app/route/data.route.js')(app);

const server = app.listen(8081, function () {
  const addr = server.address() as AddressInfo; // 类型断言
  const host = addr.address;
  const port = addr.port;
  console.log("App listening at http://%s:%s", host, port);
});

方案2:类型守卫(严谨处理两种场景)

如果需要兼容UNIX套接字的情况,用类型守卫做分支判断,确保类型安全:

import express from 'express';
import bodyParser from 'body-parser';
import db from './app/config/db.config.js';

const app = express();
app.use(bodyParser.json());

db.sequelize.sync({ force: true }).then(() => {
  console.log('Drop and Resync with { force: true }');
});

require('./app/route/data.route.js')(app);

const server = app.listen(8081, function () {
  const addr = server.address();
  if (typeof addr === 'object' && addr !== null) {
    // 处理TCP端口场景
    const host = addr.address;
    const port = addr.port;
    console.log("App listening at http://%s:%s", host, port);
  } else {
    // 处理UNIX套接字场景
    console.log("App listening at unix://%s", addr);
  }
});

方案3:简化的类型推断(快速解决)

如果你不想导入额外类型,也可以直接用类型缩小的方式快速解决:

const server = app.listen(8081, function () {
  const { address, port } = server.address() as { address: string; port: number };
  console.log("App listening at http://%s:%s", address, port);
});

额外提示

如果是从JS迁移到TS,建议逐步替换require为import,并安装对应的类型包:

npm install @types/express @types/body-parser --save-dev

内容的提问来源于stack exchange,提问作者teddy1995

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 08:37:51