Typescript 编译需要很长时间才能运行,所以我使用generateTrace了https://github.com/microsoft/TypeScript/pull/40063
它表明大多数时间是将复杂的类与其子类进行比较。例如,其中一个类是基本 Objection 模型(https://github.com/vincit/objection.js),它是我所有模型继承的基类。
例如我有这样的代码:
class User extends Model {}
function doSomething(model: typeof Model) {}
doSomething(User);
Run Code Online (Sandbox Code Playgroud)
我有大约 50 个模型。我认为 TS 第一次遇到每个模型时很慢,然后它会缓存它。TS将特定模型与基础进行比较大约需要5s Model。Model使用多个库中的类型。这是跟踪的屏幕截图,仅将每个模型与 进行比较就需要 5 分钟Model:
有没有办法让 TS 跳过Model与自身的比较?即自从Userextends以来Model,不需要检查它继承的字段。
编辑:
我将检查 50 个模型的时间从 5 分钟减少到 30 秒。我有一张模型图:
type ModelType = 'user' | ...
type ModelsMap = {
user: User,
...
};
getModel<T extends ModelType>(type: T): ModelsMap[T] {}
Run Code Online (Sandbox Code Playgroud)
这很慢,因为它ModelsMap[T]是所有模型的联合。T如果我在是所有模型类型的并集时返回基本模型,它会变得更快:
type TypeToModel<T …Run Code Online (Sandbox Code Playgroud) javascript node.js typescript tsconfig typescript-compiler-api
请考虑这个例子:
// all properties in Item should be optional, this is by design
type Item = {
id?: number
name?: string
}
interface WithVersion {
version: number
}
export type ResultType =
& WithVersion // #1 try to remove it
& {
version: number
list: Item[];
};
export interface Data {
list: Array<string>;
}
// As per my understanding, this function should not compile, because elem.list is not assignable to list in ResultType
const builder = <T extends Data>(data: Array<T>): …Run Code Online (Sandbox Code Playgroud) 是否可以从参数中获取 IP 地址HttpRequest?
这是我的代码:
#[get("/collect")]
pub async fn collect(req: HttpRequest) -> impl Responder {
println!("collect {:?}", req);
HttpResponse::Ok()
}
Run Code Online (Sandbox Code Playgroud)
[dependencies]
actix-web = "3"
Run Code Online (Sandbox Code Playgroud) 我已经开始学习 Scala。
我很惊讶下一个代码编译:
object Hello extends App {
def isOne(num: Int) = num match {
case 1 => "hello"
}
}
Run Code Online (Sandbox Code Playgroud)
例如,你不能在 Rust 中做类似的事情。
为什么 Scala 编译器不强制我为 提供默认值case?
我会说它有点不安全。
是否有任何 Scala linter 或其他东西?也许一些标志?
scala compiler-errors pattern-matching non-exhaustive-patterns
我一直认为 TypeScript 不会跟踪对象突变。例如:
type DescribableObject = {
name: string;
age: number;
};
// error
const obj: DescribableObject = {
name: 'sdf'
}
obj.age = 2
Run Code Online (Sandbox Code Playgroud)
但是,似乎在某些情况下它会跟踪函数静态属性的突变。
type DescribableFunction = {
description: string;
(): boolean;
};
// error
const fn: DescribableFunction = () => true
//fn.description = 'hello';
Run Code Online (Sandbox Code Playgroud)
如果取消注释//fn.description = 'hello';,TypeScript 错误将消失。
此外,如果您将鼠标悬停在上面,fn您会看到 TS 将其fn视为某种module.
fn函数是一个什么样的模块?这种行为有记录吗?
基本上我只是想包装一个具有多个签名的函数。有没有一种干净的方法可以做到这一点,而无需重新硬编码所有签名?我只想bar在下面接受任何相同的论点foo
declare function foo(x: string): number;
declare function foo(x: string, y: number): 42;
declare function foo(x: number): string;
type fooArgs = Parameters<typeof foo>; // this only extracts from the last signature
declare function bar(...args: fooArgs): string[]
const a = foo(123); // fine
const b = foo('123'); // fine
const c = foo('123', 456); // fine
const x = bar(123); // fine
const y = bar('123'); // fails because it expects (x: number) => string
const z = …Run Code Online (Sandbox Code Playgroud) 当我向数组提交一个新的 Person 并将其显示在表行中时,我一直试图让 renderUserList 正常工作。为了测试它是否从一开始就渲染,我从一开始就将第一个人添加到数组中。当我控制台记录数组和渲染函数本身时,它们似乎可以工作。当我“提交”某些内容时,数组会增加并且站点会重新呈现。然而,无论我做什么,我的页面仍然是空的,只有数组似乎真正按预期工作,但我的 html dom 没有丝毫改变。
class Person {
public Firstname: string;
public Lastname: string;
constructor(Firstname: string, Lastname: string) {
this.Firstname = Firstname;
this.Lastname = Lastname;
}
}
let people: Person[] = [
new Person("Peter", "Parker")]
document.addEventListener("DOMContentLoaded", () => {
renderUserList(people);
document.getElementById("add-user").addEventListener("submit", (event) => {
event.preventDefault();
const FormField: HTMLFormElement = document.getElementById("add-person") as
HTMLFormElement
const FirstNameInput: HTMLInputElement = document.getElementById("Firstname") as
HTMLInputElement;
const LastNameInput: HTMLInputElement = document.getElementById("Lastname") as
HTMLInputElement;
const FName: string = FirstNameInput.value;
const LName: string = LastNameInput.value;
if …Run Code Online (Sandbox Code Playgroud) 作为打字稿新手用户,我什至在提出问题时都遇到困难,所以请耐心等待。
我试图创建一个 key => [string + valueObject 接口] 字符串和 valueObjects (作为类型)的映射,然后有一个函数,它根据传递的键强制执行 valueObject 接口。
我觉得最好用一个例子来解释:
// This is an pseudo example stub, not actually working
type ReplaceableWith<T> = string;
// ^ the type I'd like to enforce as the argument
const templates = {
// templateId // template // define somehow the interface required for this template
'animal.sound': 'A {animal} goes {sound}' as ReplaceableWith<{ animal: string; sound: string}>
};
function renderTemplate(
templateId , // must be a key of templates
params …Run Code Online (Sandbox Code Playgroud) 是否允许在 React 上下文中存储不可序列化的对象?
我无法在文档中找到这个问题的答案
我知道这在 redux store 中被认为是一种不好的做法,请参阅此处。
您能否提供有关在上下文中使用不可序列化对象的任何参数或文档链接?
考虑下一个例子:
class A {
foo=()=> void 0
bar=()=> void 0
}
const context = React.createContext(new A()) // is this ok ?
Run Code Online (Sandbox Code Playgroud) 您能否使用小而简单的 TypeScript 示例来解释什么是方差、协方差、逆变和双方差?
[持续更新]
有用的链接:
Oleg Valter 的另一个与该主题相关的好答案
Titian-Cernicova-Dragomir对*-rianance 的很好解释
斯蒂芬博耶博客
Scala 文档- 用例子很好的解释
@Titian 的回答 1
@Titian 的回答 2
马克西曼的文章
typescript ×7
javascript ×2
actix-web ×1
arrays ×1
covariance ×1
generics ×1
html ×1
invariance ×1
node.js ×1
overloading ×1
react-redux ×1
reactjs ×1
redux ×1
renderer ×1
rust ×1
scala ×1
tsconfig ×1
variance ×1