所以我有一个基于此的 monorepo 尝试使用:
这个仓库有 4 个独立的地方,其中有一个 package.json(理论上是一个 tsconfig.json)——该应用程序部分是 TypeScript,部分是 Javascript(随着时间的推移,逐渐用 TypeScript 重写它)
基本上目录结构如下所示:
/ - root directory
/apps/expo - expo-related packages, configurations, functions, etc
/apps/next - next-related packages, configuration, functions, etc
/packages/app/ - general business logic, code, pages, etc
Run Code Online (Sandbox Code Playgroud)
无论我尝试做什么或尝试在何处设置路由,它都不起作用。
在我的根文件夹中的 tsconfig.json 中,我有这个(我也尝试将其放入各个文件夹的各个 tsconfig.json 文件中):
"paths": {
"app/*": ["./packages/app/*"],
"components/*": ["./packages/app/components"],
"config/*": ["./packages/app/config"],
"controllers/*": ["./packages/app/controllers"],
"pages/*": ["./packages/app/pages"],
"reducers/*": ["./packages/app/redux"],
"resources/*": ["./packages/app/resources"],
"revenuecat/*": ["./packages/app/revenuecat"],
"routing/*": ["./packages/app/routing"],
"utils/*": ["./packages/app/utils"],
"interfaces/*": ["./packages/app/interfaces"],
"root/*": ["./*"]
}
Run Code Online (Sandbox Code Playgroud)
但我的 Next 应用程序无法识别这些路径。
我尝试将 Expo 文件夹的 babel.config.js …
我正在将一个功能性 mixin 项目从 javascript 迁移到 typescript。我所有的 javascript minxins 都有带有单个参数的构造函数签名constructor(props){}。
在打字稿中,我按照https://www.typescriptlang.org/docs/handbook/mixins.html的官方文档定义了 mixin 构造函数类型 :
export type ConstrainedMixin<T = {}> = new (...args: any[]) => T;
Run Code Online (Sandbox Code Playgroud)
即使我将该 mixin 签名更改为:
export type ConstrainedMixin<T = {}> = new (props: any) => T;
Run Code Online (Sandbox Code Playgroud)
并更新实现 TSC 将抛出错误:
TS2545:mixin 类必须具有一个带有“any[]”类型的单个剩余参数的构造函数。
不幸的是,它无法为传递给构造函数的参数创建唯一的类型签名。我现在还需要迁移所有现有的构造函数。如何为 mixin 构造函数创建更明确的类型接口?
我创建了一个游乐场示例
您可以在此屏幕截图中看到编译器在 MIXIN 定义上出现错误,并表示 mixin 类必须具有单个剩余参数。即使类型定义是:
type ConstrainedMixin<T = {}> = new (props: any) => T;
Run Code Online (Sandbox Code Playgroud)
在我的示例中,const mg = new MixedGeneric({cool: 'bro'});
我想为该{cool: 'bro'}对象创建一个接口,并从 mixin 定义中强制执行它。我不确定如何正确执行此操作。如果构造函数是 …
我遇到了这个奇怪的案例。我声明了一个条件类型。对于相同的extends约束,类型别名满足它,而结构相同的接口则不满足。
我很失落,为什么不同?检查操场。
interface Constraint {
[key: string]: string | number | boolean
}
type ATypeAlias = {
str: string
num: number
bool: boolean
}
interface SameInterface {
str: string
num: number
bool: boolean
}
type expectToBeTrue = ATypeAlias extends Constraint ? true : false
// Wat???
type butWhyAmIFalse = SameInterface extends Constraint ? true : false
Run Code Online (Sandbox Code Playgroud) 我相信我的理解数学的Y组合子的想法:它返回的功能给出一个固定点F,从而f = Y(F)在那里f满足f == F(f).
但我不明白实际的计算程序是如何明智的?
我们来看一下这里给出的javascript示例:
var Y = (F) => ( x => F( y => x(x)(y) ) )( x => F( y => x(x)(y) ) )
var Factorial = (factorial) => (n => n == 0 ? 1 : n * factorial(n-1))
Y(Factorial)(6) == 720 // => true
computed_factorial = Y(Factorial)
Run Code Online (Sandbox Code Playgroud)
我不明白的部分是如何computed_factorial实际计算函数(固定点)?通过跟踪Y的定义,你会发现它在该部分遇到无限递归x(x),我看不到那里隐含的任何终止案例.然而,奇怪的确回来了.谁能解释一下?
给定一个类型的对象:
type Key2Value = {
foo: "bar"
voo: "doo"
}
Run Code Online (Sandbox Code Playgroud)
假设该类型的值始终是字符串类型,那么如何构造一个ReverseMap<T>将键值对反向映射到值键对的实用程序类型?
type Value2Key = ReverseMap<Key2Value>
// yields:
type Value2Key = {
bar: "foo"
doo: "voo"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将长生不老药的演员模型语言原语移植到 JS 中。我想出了一个解决方案(在 JS 中)来模拟receiveelixir 关键字,使用“接收器”函数和生成器。
这是一个简化的实现和演示,向您展示这个想法。
应用程序接口:
type ActorRef: { send(msg: any): void }
type Receiver = (msg: any) => Receiver
/**
* `spawn` takes a `initializer` and returns an `actorRef`.
* `initializer` is a factory function that should return a `receiver` function.
* `receiver` is called to handle `msg` sent through `actorRef.send(msg)`
*/
function spawn(initializer: () => Receiver): ActorRef
Run Code Online (Sandbox Code Playgroud)
演示:
function* coroutine(ref) {
let result
while (true) {
const msg = …Run Code Online (Sandbox Code Playgroud)我可以在 HTML 中的标记JSDoc中使用script,但无法引用类型定义文件 ( .d.ts)。我在用着VS Code。这可能吗?如果可以我该怎么做?下面是一些示例代码:
该tsconfig.json文件(我不确定如何在我的测试中完全使用它,它似乎不需要。):
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"myLib": ["types/MyLib/index.d.ts"],
"myLib2": ["types/MyLib/index2.d.ts"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
在types/MyLib/index.d.ts:
declare namespace myLib {
function makeGreeting(s: string): string
let numberOfGreetings: number
}
Run Code Online (Sandbox Code Playgroud)
在 JS 文件中index.js,工作正常:
/// <reference path="./types/MyLib/index.d.ts" />
// @ts-check
var result = myLib.makeGreeting("Cheese")
console.log(result)
Run Code Online (Sandbox Code Playgroud)
在 HTML 文件中(这些都不起作用)index.html:
<script>
/// <reference path="./types/MyLib/index.d.ts" />
// @ts-check
var result = myLib.makeGreeting("Cheese")
</script>
<script> …Run Code Online (Sandbox Code Playgroud) 我想使用 Chai 作为断言库,而不是 Jest。我使用打字稿,我想用 Chai 期望的类型替换全局 Jest 期望。
我尝试做类似的事情:
import chai from "chai";
type ChaiExpect = typeof chai.expect;
declare global {
export const expect: ChaiExpect;
}
global.expect = chai.expect;
Run Code Online (Sandbox Code Playgroud)
但打字稿抱怨是因为:
Cannot redeclare block-scoped variable 'expect'.ts(2451)
index.d.ts(39, 15): 'expect' was also declared here.
Run Code Online (Sandbox Code Playgroud)
如何覆盖 jest 内部index.d.ts声明的类型?
typescript ×6
javascript ×3
chai ×1
closures ×1
html ×1
jestjs ×1
jsdoc ×1
react-native ×1
reactjs ×1
v8 ×1
webpack ×1
y-combinator ×1