我有一个简单的应用程序,带有 HTTP 端点和到 MongoDB 数据库的连接。
use actix_web::{
middleware, post,
web::{self},
App, HttpServer, Responder,
};
use mongodb::{options::ClientOptions, Client};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct TestBody {
name: String,
age: u8,
}
#[post("/test")]
async fn test(query: web::Json<TestBody>, db: web::Data<Client>) -> impl Responder {
for db_name in db.list_database_names(None, None).await.unwrap() {
println!("{}", db_name);
}
let res = format!("{} {}", query.name, query.age);
res
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let connection_string = "secret-connection-string";
let client_options = ClientOptions::parse(connection_string).await.unwrap();
let client = Client::with_options(client_options).unwrap();
HttpServer::new(move || { …Run Code Online (Sandbox Code Playgroud) 在查看 Rust 结构体的文档时,我经常会遇到标题为“Blanket Implements”的部分。我听说它可以用来实现所有类型或匹配某些条件的所有类型的特征,但我不确定为什么需要这样做。
那么一揽子实现到底是什么?为什么它们在 Rust 中有用?
enum AllowedFruits {
Apple = 'APPLE',
Banana = 'BANANA',
Pear = 'PEAR'
}
const allowedFruits: AllowedFruits[] = [
AllowedFruits.Apple, AllowedFruits.Banana, AllowedFruits.Pear
]
Run Code Online (Sandbox Code Playgroud)
我想要实现的是将数组限制为具有特定枚举的每个字段。我期望allowedFruits通过添加或删除 的字段来显示类型错误AllowedFruits。
有什么办法可以实现吗?
如果有任何文章或文档可供我参考,请告诉我。
我正在使用Bun.sh创建一个 React 应用程序。
但我也使用 tailwindcss 进行造型,而 tailwind 对于发髻没有官方解决方案。如何将这两者结合使用?
我知道面包还没有准备好生产,但如果可能的话,我仍在寻找解决方案。
如何正确输入我的 useState 挂钩?
我有这种enum类型:
export enum Status {
PENDING = 'pending',
SUCCESS = 'success',
ERROR = 'error',
}
Run Code Online (Sandbox Code Playgroud)
还有useState钩子:const [isValid, setIsValid] = useState<// What to add here>(ApiStatus.PENDING);
那么useState钩子的值只能是其中一个Status值?
将 @nestjs/mongoose 从 9.0.3 更新到 9.1.0 后,我遇到以下错误:
[Nest] 3818 - 06/04/2022, 12:50:13 AM ERROR [ExceptionHandler] Nest can't resolve dependencies of the WatchlistService (?). Please make sure that the argument PortfolioModel at index [0] is available in the AppModule context.
Potential solutions:
- If PortfolioModel is a provider, is it part of the current AppModule?
- If PortfolioModel is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing PortfolioModel */ ]
}) …Run Code Online (Sandbox Code Playgroud) 在下面的代码片段中,我订阅了一个投资组合并遍历一个数组。对于数组的每个成员,我创建另一个订阅来收听其报价。
this.portfolio$
.subscribe(portfolio) => {
portfolio.watchlist.all.forEach((a) => {
this.store
.select((s) => s.quotes.quotes[a._id])
.subscribe((q) => {
console.warn('subscription')
}
}
})
})
Run Code Online (Sandbox Code Playgroud)
是否有任何 RxJS 运算符可以让我摆脱外部订阅?最后,我只想拥有与数组中的引号一样多的订阅。
我想知道是否可以在打字稿中键入对象中动态属性的最大数量。
所以基本的例子是跟踪事件:
events.track('SOME_EVENT', { first: 'a', other: 'b', some: 'c'})
Run Code Online (Sandbox Code Playgroud)
事件数据应该最多保存 3 个属性及其各自的值,键也可以是动态的。
我用 basic 键入它Record,但允许的属性数量没有限制:
export interface Events {
track: (name: string, params?: Record<string, string | number | unknown>) => void;
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
I'm trying to learn Typescript with React, but this error got in my way. I don't understand why is it even happening. Here it is. Creating an array of strings that looks like colors array down there. I want to create types based on values of that array ("white" | "red" | "blue").
const colors = ["white", "red", "blue"] as const;
type Colors= typeof colors[number];
Run Code Online (Sandbox Code Playgroud)
If I do it like this, it works, Colors has wanted types
const colorsArr = …Run Code Online (Sandbox Code Playgroud) typescript ×5
javascript ×3
reactjs ×3
rust ×2
actix-web ×1
angular ×1
bun ×1
enums ×1
mapped-types ×1
mongoose ×1
nestjs ×1
ngrx ×1
react-hooks ×1
rxjs ×1
tailwind-css ×1
types ×1