在下面的示例代码中,TypeScript 无法推断函数中的泛型类型U,fooBar因此返回类型fooRef.doIt()为unknown。为什么会这样,我需要修改什么才能获得正确的返回类型?
interface Foo<T> {
foo(): T;
}
class Bar implements Foo<string> {
foo(): string {
return 'Bar';
}
}
class Baz implements Foo<number> {
foo(): number {
return 43;
}
}
class FooRef<T extends Foo<U>, U> {
constructor(private instance: T) {}
doIt() {
return this.instance.foo();
}
}
function fooBar<T extends Foo<U>, U>(foo: new (...args: any[]) => T) {
return new FooRef(new foo());
}
const barRef = fooBar(Bar);
barRef.doIt(); // unknown, expected …Run Code Online (Sandbox Code Playgroud) 运行 Angular Jest 测试时,出现以下错误:
"Type InButtonComponent is part of the declarations of 2 modules: InButtonModule and InButtonModule! Please consider moving InButtonComponent to a higher module that imports InButtonModule and InButtonModule. You can also create a new NgModule that exports and includes InButtonComponent then import that NgModule in InButtonModule and InButtonModule."
Run Code Online (Sandbox Code Playgroud)
我以前遇到过“2 个模块的声明”问题,通常很容易解决;不要在两个不同的模块中声明相同的组件。但是,在这种情况下,我不会InButtonComponent在两个不同的模块中声明。就像 Angular 抱怨我在同一个模块中声明了两次组件?该错误消息对我来说没有任何意义。
无论我是为产品构建还是运行开发服务器,我都不会在应用程序中收到此错误。然而,当我运行 Jest 测试时,它们失败了。
如果有帮助的话,这里是一些可能涉及的文件:
// button.component.ts single file SCAM
// ...
@NgModule({
declarations: [InButtonComponent],
imports: [CommonModule],
exports: [InButtonComponent],
})
export class InButtonModule {}
Run Code Online (Sandbox Code Playgroud)
// …Run Code Online (Sandbox Code Playgroud) 我有一个 .NET 7.0 应用程序,其中使用 Autofac 作为我的 DI 容器。我有两个接口IReadonlyRepository<T>和IRepository<T>,其中IRepository<T>继承自IReadonlyRepository<T>.
我的存储库类实现了这些接口。我还有一个装饰器 ,LoggingRepository<T>它包装IRepository<T>、记录对存储库方法的每个调用,并将调用委托给包装的IRepository<T>实例。这是代码LoggingRepository<T>:
class LoggingRepository<T> : IRepository<T>
{
private readonly IRepository<T> _repository;
public LoggingRepository(IRepository<T> repository)
{
_repository = repository;
}
public IEnumerable<T> List()
{
Console.WriteLine($"Calling {_repository.GetType().Name}<{typeof(T).Name}>.List()");
return _repository.List();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我在 DI 容器中注册类和装饰器的方法:
builder.RegisterGeneric(typeof(Repository<>))
.As(typeof(IRepository<>))
.As(typeof(IReadonlyRepository<>))
.InstancePerLifetimeScope();
builder.RegisterGenericDecorator(typeof(LoggingRepository<>), typeof(IRepository<>));
builder.RegisterGenericDecorator(typeof(LoggingRepository<>), typeof(IReadonlyRepository<>));
Run Code Online (Sandbox Code Playgroud)
我还尝试使用命名注册来潜在地规避此问题,但它并没有改变行为。
我面临的问题是,当我请求 的实例时IReadonlyRepository<T>,似乎LoggingRepository<T>最终装饰了另一个实例LoggingRepository<T>,而该实例又装饰了该Repository<T>实例。这不是预期的行为 - 我只想LoggingRepository<T>装饰的单个实例Repository<T> …
这是一堂课:
export class Survey {
isCompleted: boolean;
response: {
'devices': string[];
'languages': string[];
'frameworks': string[];
'backend': string[];
};
}
Run Code Online (Sandbox Code Playgroud)
我在尝试以下内容时得到" 元素隐式具有'任何'类型,因为类型'...'没有索引签名 "错误:
return Object.keys(user1.response).map(key => {
return percentageMatch(user1.response[key], user2.response[key]) * categoryScores[key];
})
Run Code Online (Sandbox Code Playgroud)
user1并且user2是Survey班级的实例.
我知道如何使用简单的对象文字设置索引签名,但是如何使用对象的属性来实现,response对象本身就是Survey类的属性.
我正在尝试一起实现一个通用的规范模式和一个通用的访问者模式。这是我的基本接口。
export interface Specification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> {
accept(visitor: TVisitor): void;
isSatisfiedBy(candidate: T): boolean;
and(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
andNot(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
or(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
orNot(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
not(): Specification<T, TVisitor>;
}
export interface SpecificationVisitor<TVisitor extends SpecificationVisitor<TVisitor, T>, T> {
visit(specification: AndSpecification<T, TVisitor>): void;
visit(specification: AndNotSpecification<T, TVisitor>): void;
visit(specification: OrSpecification<T, TVisitor>): void;
visit(specification: OrNotSpecification<T, TVisitor>): void;
visit(specification: NotSpecification<T, TVisitor>): void;
}
Run Code Online (Sandbox Code Playgroud)
为方便起见,我为基本布尔运算符实现了一些基类和一个抽象类。
export abstract class CompositeSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> implements Specification<T, TVisitor> …Run Code Online (Sandbox Code Playgroud) generics types visitor-pattern specification-pattern typescript
我正在使用 Angular dotnet 模板,它利用了与 Spa 相关的扩展。
为了让 SPA 服务于localhost:5001/app而不是 ,我需要进行哪些更改localhost:5001?
Startup.cs 文件如下所示:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace angular_dotnet
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// In production, the Angular files …Run Code Online (Sandbox Code Playgroud) 我正在使用Heroku管道工作流,该工作流使用动态生成的URL部署审阅应用程序。例如,假设我的生产应用程序已部署在mykickassapp.herokuapp.com上,每次我在存储库中进行PR时,Heroku都会自动将其PR部署在mykickassapp-pr-50.herokuapp.com之类的位置。
在我的客户端代码中,我利用环境文件并将应用程序URL存储在上面的示例中:mykickassapp.herokuapp.com。问题在于,它显然不适用于以不同动态生成的URL部署的审阅应用程序。幸运的是,Heroku将应用程序的URL存储在一个环境变量中。但是问题是如何将其引入Angular应用程序的客户端?
我的应用程序使用标准的Angular-CLI生产构建过程。
在 JavaScript 中实现队列时有很多选择。一种可能的实现是使用数组,使用 Array.push 将元素添加到队列中,并使用 Array.shift 从队列中删除它们。另一种可能的实现是使用链接列表并添加到尾部并从头部删除。
通过测试 Stacks 实现,我确定 Array.push 和 Array.pop,无论数组大小如何,都比在链表尾部添加或删除元素快 3-5 倍。因此,在JavaScript中,使用链表来实现堆栈是没有意义的。但我想知道,在队列的情况下,使用 Array.shift 从数组前面删除元素与从列表前面删除元素在性能上有什么区别?
在简单的单元测试中使用mockgoose非常简单.但是,对于如何在验收或集成测试中使用mockgoose或其他模拟解决方案,我有点模糊.
给出一个简单的express/MongoDB应用程序,如下所示:
/*app.js*/
const express = require('express')
const app = express()
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var greetingSchema = mongoose.Schema({
greeting: String
});
var Greeting = mongoose.model('Greeting', greetingSchema);
app.get('/', function (req, res) {
Greeting.find({greeting: 'Hello World!'}, function (err, greeting){
res.send(greeting);
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Run Code Online (Sandbox Code Playgroud)
和这样的简单集成测试:
/*test.js*/
const app = require('app.js');
const request = require('supertest');
it('sends "Hello World!" on the response body', (done) => {
request(app)
.get('/')
.expect(200, 'Hello World!', done);
}); …Run Code Online (Sandbox Code Playgroud)