'PassportStatic' 类型的参数不可分配给 'Passport' 类型的参数。 属性 'use' 的类型不兼容
Argument of type 'PassportStatic' is not assignable to parameter of type 'Passport'. Types of property 'use' are incompatible
我正在尝试在我的应用程序中实现登录功能,我正在使用护照进行身份验证。但是我在打字稿编译时遇到错误:
TSError: ⨯ Unable to compile TypeScript
server/server.ts (132,26): Argument of type 'PassportStatic' is not
assignable to parameter of type 'Passport'.
Types of property 'use' are incompatible.
Type '{ (strategy: Strategy): PassportStatic; (name: string, strategy: Strategy): PassportStatic; }' is not assignable to type '{ (strategy: Strategy): this; (name: string, strategy: Strategy): this; }'.
Type 'PassportStatic' is not assignable to type 'this'. (2345)
at getOutput (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:307:15)
at /media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:336:16
at Object.compile (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:496:11)
at Module.m._compile (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:392:43)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:395:12)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
这是我的 server.ts 代码:
import * as bodyParser from 'body-parser';
import * as connectMongo from 'connect-mongo';
import * as express from 'express';
import * as session from 'express-session';
import * as cookieParser from 'cookie-parser';
import * as httpLogger from 'morgan';
import * as mongoose from 'mongoose';
import * as passport from 'passport';
import * as path from 'path';
// Config
import { Config } from './config/local';
import { PassportConfig } from './config/passport';
import { logger } from './config/logger';
/**
* The server.
*
* @class Server
*/
export class Server {
// The express app instance
public app: express.Application;
/**
* Bootstrap the application
*
* @class Server
* @method bootstrap
* @static
* @return {ng.auto.IInjectorService} Returns the newly created injector for this app
*/
public static bootstrap = (): Server => {
return new Server();
};
/**
* Constructor.
*
* @class Server
* @constructor
*/
constructor() {
// create expressjs application
this.app = express();
// configure application
this.config();
this.routes();
}
/**
* Configure application
*
* @class Server
* @method config
*/
public config = (): void => {
this.app.use(httpLogger('dev'));
// use json bodyparser
this.app.use(bodyParser.json());
// use query string parser
this.app.use(bodyParser.urlencoded({
extended: true
}));
// use CookieParser for setting and reading cookies
this.app.use(cookieParser(Config.session_secret));
/* some code skipped */
// Set up passport
**PassportConfig.setup(passport, this.userModel);**
this.app.use(passport.initialize());
this.app.use(passport.session()); // persistent login sessions
}
/**
* Create router
*
* @class Server
* @method routes
*/
public routes = () => {
// Backend Admin API Routes
let prefix = '';
//this.app.use(`/accounts`, LoginRoute.create(this.model.localUser, passport));
this.app.use('/data', DataRoute.create());
this.app.use('/files', express.static(path.join(__dirname, 'files')));
};
/**
* Shutdown
*
* @class Server
* @method shutdown
*/
public shutdown = () => {
logger.info('Shutting Down');
this.connection.close();
}
}
这是我的 PassportConfig 代码:
import { Model } from 'mongoose';
import { Passport } from 'passport';
import { Strategy, IStrategyOptions, VerifyFunction } from 'passport-local';
import { IUserModel } from '../models/user';
export class PassportConfig {
static setup = (passport: Passport, model: Model<IUserModel>) => {
// serialize by username as it is unique <Type of user, type of id>
passport.serializeUser<IUserModel, string>((user: IUserModel, done) => {
// Return unique identification of the user
done(null, user._id);
});
// deserialize by username <Type of user, typeof of id>
passport.deserializeUser<IUserModel, string>((id: string, done) => {
// findbyid and return user
model.findById(id, (err, user: IUserModel) => {
done(err, user);
});
});
// Specify strategy options
let options: IStrategyOptions = {
usernameField: 'username',
passwordField: 'password',
passReqToCallback: false
};
// verify function for signup
let verifySignUp: VerifyFunction = (username: string, password: string, done) => {
process.nextTick(() => {
model.findOne({
'username': username
}, (err, user: IUserModel) => {
if (err) {
return done(err);
}
if (user) {
return done(err, null);
} else {
let newUser = new model();
newUser.username = username;
newUser.password = newUser.generateHash(password);
// save the user
newUser.save((error) => {
return done(error, newUser);
});
}
});
});
};
let verifySignIn: VerifyFunction = (username: string, password: string, done) => {
process.nextTick(() => {
model.findOne({
'username': username
}, (err, user: IUserModel) => {
if (err)
return done(err);
if (!user) {
return done(null, null);
} else if (!user.validPassword(password)) {
return done(null, null);
}
return done(null, user);
});
});
};
passport.use('local-signup', new Strategy(options, verifySignUp));
passport.use('local-signin', new Strategy(options, verifySignIn));
}
}
我的 Webstorm IDE 在我的 server.ts 文件中的 PassportConfig.setup(passport, this.userModel);
行的 passport
下给出了红色下划线。
请帮忙。
仅根据导入进行猜测,但看起来您正在从 server.ts
中的 passport
库中导入所有内容:
import * as passport from 'passport';
所以变量 passport
的类型是 PassportStatic
.
并且在 PassportConfig
中,您正在导入从库 passport
:
中导出的混凝土 class Passport
import { Passport } from 'passport';
所以变量 Passport
的类型是 Passport
.
要解决这个问题,您可以做几件事:
- 以与
PassportConfig
相同的方式在 server.ts
中导入 Passport
- 在
server.ts
中使用 passport.Passport
(而不仅仅是 Passport
)
我正在尝试在我的应用程序中实现登录功能,我正在使用护照进行身份验证。但是我在打字稿编译时遇到错误:
TSError: ⨯ Unable to compile TypeScript
server/server.ts (132,26): Argument of type 'PassportStatic' is not
assignable to parameter of type 'Passport'.
Types of property 'use' are incompatible.
Type '{ (strategy: Strategy): PassportStatic; (name: string, strategy: Strategy): PassportStatic; }' is not assignable to type '{ (strategy: Strategy): this; (name: string, strategy: Strategy): this; }'.
Type 'PassportStatic' is not assignable to type 'this'. (2345)
at getOutput (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:307:15)
at /media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:336:16
at Object.compile (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:496:11)
at Module.m._compile (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:392:43)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:395:12)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
这是我的 server.ts 代码:
import * as bodyParser from 'body-parser';
import * as connectMongo from 'connect-mongo';
import * as express from 'express';
import * as session from 'express-session';
import * as cookieParser from 'cookie-parser';
import * as httpLogger from 'morgan';
import * as mongoose from 'mongoose';
import * as passport from 'passport';
import * as path from 'path';
// Config
import { Config } from './config/local';
import { PassportConfig } from './config/passport';
import { logger } from './config/logger';
/**
* The server.
*
* @class Server
*/
export class Server {
// The express app instance
public app: express.Application;
/**
* Bootstrap the application
*
* @class Server
* @method bootstrap
* @static
* @return {ng.auto.IInjectorService} Returns the newly created injector for this app
*/
public static bootstrap = (): Server => {
return new Server();
};
/**
* Constructor.
*
* @class Server
* @constructor
*/
constructor() {
// create expressjs application
this.app = express();
// configure application
this.config();
this.routes();
}
/**
* Configure application
*
* @class Server
* @method config
*/
public config = (): void => {
this.app.use(httpLogger('dev'));
// use json bodyparser
this.app.use(bodyParser.json());
// use query string parser
this.app.use(bodyParser.urlencoded({
extended: true
}));
// use CookieParser for setting and reading cookies
this.app.use(cookieParser(Config.session_secret));
/* some code skipped */
// Set up passport
**PassportConfig.setup(passport, this.userModel);**
this.app.use(passport.initialize());
this.app.use(passport.session()); // persistent login sessions
}
/**
* Create router
*
* @class Server
* @method routes
*/
public routes = () => {
// Backend Admin API Routes
let prefix = '';
//this.app.use(`/accounts`, LoginRoute.create(this.model.localUser, passport));
this.app.use('/data', DataRoute.create());
this.app.use('/files', express.static(path.join(__dirname, 'files')));
};
/**
* Shutdown
*
* @class Server
* @method shutdown
*/
public shutdown = () => {
logger.info('Shutting Down');
this.connection.close();
}
}
这是我的 PassportConfig 代码:
import { Model } from 'mongoose';
import { Passport } from 'passport';
import { Strategy, IStrategyOptions, VerifyFunction } from 'passport-local';
import { IUserModel } from '../models/user';
export class PassportConfig {
static setup = (passport: Passport, model: Model<IUserModel>) => {
// serialize by username as it is unique <Type of user, type of id>
passport.serializeUser<IUserModel, string>((user: IUserModel, done) => {
// Return unique identification of the user
done(null, user._id);
});
// deserialize by username <Type of user, typeof of id>
passport.deserializeUser<IUserModel, string>((id: string, done) => {
// findbyid and return user
model.findById(id, (err, user: IUserModel) => {
done(err, user);
});
});
// Specify strategy options
let options: IStrategyOptions = {
usernameField: 'username',
passwordField: 'password',
passReqToCallback: false
};
// verify function for signup
let verifySignUp: VerifyFunction = (username: string, password: string, done) => {
process.nextTick(() => {
model.findOne({
'username': username
}, (err, user: IUserModel) => {
if (err) {
return done(err);
}
if (user) {
return done(err, null);
} else {
let newUser = new model();
newUser.username = username;
newUser.password = newUser.generateHash(password);
// save the user
newUser.save((error) => {
return done(error, newUser);
});
}
});
});
};
let verifySignIn: VerifyFunction = (username: string, password: string, done) => {
process.nextTick(() => {
model.findOne({
'username': username
}, (err, user: IUserModel) => {
if (err)
return done(err);
if (!user) {
return done(null, null);
} else if (!user.validPassword(password)) {
return done(null, null);
}
return done(null, user);
});
});
};
passport.use('local-signup', new Strategy(options, verifySignUp));
passport.use('local-signin', new Strategy(options, verifySignIn));
}
}
我的 Webstorm IDE 在我的 server.ts 文件中的 PassportConfig.setup(passport, this.userModel);
行的 passport
下给出了红色下划线。
请帮忙。
仅根据导入进行猜测,但看起来您正在从 server.ts
中的 passport
库中导入所有内容:
import * as passport from 'passport';
所以变量 passport
的类型是 PassportStatic
.
并且在 PassportConfig
中,您正在导入从库 passport
:
Passport
import { Passport } from 'passport';
所以变量 Passport
的类型是 Passport
.
要解决这个问题,您可以做几件事:
- 以与
PassportConfig
相同的方式在 - 在
server.ts
中使用passport.Passport
(而不仅仅是Passport
)
server.ts
中导入 Passport