小编sev*_*sev的帖子

如何在打字稿中进行顶级等待(从 commonjs 切换到 esnext),而不必更改所有导入以使其以 .js 结尾

我想在我的 typescript Nodejs 项目中有顶级的等待。

我的 tsconfig 过去看起来像这样:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
Run Code Online (Sandbox Code Playgroud)

我现在把它切换到

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
Run Code Online (Sandbox Code Playgroud)

我还添加到"type": "module"了我的 package.json 中。事实上我现在有能力做顶级的等待 …

javascript node.js typescript tsc

14
推荐指数
1
解决办法
904
查看次数

在 tailwindcss 中指定网格列/行大小

我想创建一个带有 tailwind css 的网格,其中第一列非常窄,第二列非常宽。通常我发现顺风文档非常直观,但我不理解这个。使用grid-cols-{n}我可以创建相同大小的列,但我不明白如何创建不同大小的列。我该怎么办?

tailwind-css

9
推荐指数
1
解决办法
1万
查看次数

无法将 cli 参数添加到 TypeORM 中的 DataSourceOptions

DataSourceOptions在 typeORM 文档中,可以根据https://github.com/typeorm/typeorm/blob/master/docs/data-source-options.md添加 cli 参数。我在https://typeorm.io/using-cli上看到的示例看起来是

{
    cli: {
        entitiesDir: "src/entity",
        subscribersDir: "src/subscriber",
        migrationsDir: "src/migration"
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的代码中尝试了这一点,如下所示:

let dataSource = new DataSource(
  {
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        database: 'website',
        username: 'test',
        password: 'test',
        logging: true,
        synchronize: false,
        entities: [User, Posts],
        cli: {
            entitiesDir: "src/entity",
            subscribersDir: "src/subscriber",
            migrationsDir: "src/migration"
        }
  })
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误: 类型的参数'{ type: "postgres"; host: string; port: number; database: string; username: string; password: string; logging: true; synchronize: false; entities: (typeof User | …

javascript typescript typeorm

7
推荐指数
1
解决办法
8078
查看次数

当 K 是两者的共享属性时,类型“fooType[K]”不可分配给类型“barType[K]”

在下面的代码中,我尝试对于 的每个属性foo,检查 中的属性是否barnullish,如果是,则bar使用 中的属性覆盖 的该属性foo

type fooType = {
  a?: string
  b?: number
}

type barType = fooType & {
  c: string
}

const foo: fooType = {
  a: 'hello',
  b: 5,
}

const bar: barType = {
  a: 'greetings',
  b: undefined,
  c: 'not in foo',
}

const fooKeys = Object.keys(foo) as Array<keyof typeof foo>
fooKeys.forEach((k) => {
  bar[k] ??= foo[k]
})
Run Code Online (Sandbox Code Playgroud)

我收到错误:Type 'string' is not assignable to type …

typescript typescript-generics

7
推荐指数
1
解决办法
111
查看次数

tailwind `grid-cols-[1fr,700px,2fr]` 中的参数有什么作用?

我\xe2\x80\x99m 试图更好地理解顺风网格 - 有人可以帮助我理解每个参数1fr和中的700px作用2fr

\n
\n<!-- Complex grids -->\n<div class="grid-cols-[1fr,700px,2fr]">\n  <!-- ... -->\n</div>\n\n
Run Code Online (Sandbox Code Playgroud)\n

tailwind-css

3
推荐指数
1
解决办法
4699
查看次数

使打字稿函数返回类型以输入为条件

在下面的代码中,即使我们将其用作函数的输入,也将始终分配 ab类型。有没有一种方法可以实现一些智能打字稿魔法,例如使用泛型,以便根据函数输入分配正确的类型?number | stringbstringFormat.hexb

enum Format{
  hex,
  integer
}

function fun (a: Format){
  const k = Math.floor(Math.random()*100)
  if (a === Format.hex) {
    return k.toString(16)
  }
  else { 
    return k
  }
}

const b = fun(Format.hex)
Run Code Online (Sandbox Code Playgroud)

typescript typescript-generics

2
推荐指数
1
解决办法
37
查看次数

创建仅需要指定字段的类型

假设我有一个类型

type A = {
  a: string
  b?: number
  c: boolean
  d?: string
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能从中派生出一个类型,我只需要a并且b将其他所有内容保留为可选

type A2 = {
  a: string
  b: number
  c?: boolean
  d?: string
}
Run Code Online (Sandbox Code Playgroud)

我的方法是:

export type WithRequiredOnly<T, K extends keyof T> = T & { [P in K]-?: T[P] } & { [P in Exclude<keyof T, K>]+?: T[P] }

A2 = WithRequiredOnly<A, 'a'|'b'>
Run Code Online (Sandbox Code Playgroud)

但这似乎还需要其他字段......

typescript typescript-generics

1
推荐指数
1
解决办法
47
查看次数