我对as const演员表感到困惑。我检查了一些文件和视频,但没有完全理解。
我担心的是as const下面代码中的含义是什么,使用它有什么好处?
const args = [8, 5] as const;
const angle = Math.atan2(...args);
console.log(angle);
Run Code Online (Sandbox Code Playgroud) 我有一个大型 TypeScript 项目,我在启动时没有使用"importsNotUsedAsValues": "error"编译器标志。随着项目的发展,我认为在优化方面,import type当导入不被用作值时强制执行是一个好主意。
启用此功能效果很好,甚至可以在可能的情况下使用我的 IDE 自动导入类型import types,并且它还可以自动修复不将类型仅导入为类型而不是一次性导入整个项目的打开文件,我有很多这样的情况在项目中手动修复它们是不可想象的。
我一直在阅读 ESLint、TSC 和类似的工具文档,试图找到一种可以使用它们自动修复导入的方法,但没有成功。有谁知道可以自动解决这些问题的工具吗?
在需要顶级等待之后,我现在花了一个小时寻找新错误的解决方案。到目前为止我尝试过的所有其他方法都没有解决错误,例如添加"type": "module"到 package.json 文件中。
错误消息是Cannot use import statement outside a module在启动服务时。如果我将更改恢复"module": "ESNext",为"module": "commonjs",,它工作正常(除了必须删除 await 关键字并以某种方式重构以在没有等待的情况下工作)。
另外,我使用 ts-node-dev 来运行服务,可以在 package.json 文件中看到。
包.json
{
"name": "",
"version": "1.0.0",
"description": "microservice",
"main": "src/index.ts",
"author": "",
"type": "module",
"license": "MIT",
"scripts": {
"dev": "NODE_ENV=development tsnd --respawn --files src/index.ts",
"prod": "NODE_ENV=production tsnd --respawn --transpile-only --files src/index.ts",
"test": "mocha --exit -r ts-node/register tests/**/*.spec.ts",
"eslint": "eslint src/**/*.ts"
},
Run Code Online (Sandbox Code Playgroud)
配置文件
{
"compilerOptions": {
"target": "ES2020",
"module": …Run Code Online (Sandbox Code Playgroud) 当我升级到最新版本的 TypeScript 并发现仅类型导入时,我认为它非常酷并开始在任何地方使用它。
设置仅类型导入一段时间后,我很快意识到我得到了预期的更多冗长和“脏代码”。
没有仅类型导入:
import { SomeType, someFunction, SomeClassToInstantiate } from '@my-app/lib1';
import { OtherType, otherFunction, OtherClassToInstantiate } from '@my-app/lib2';
Run Code Online (Sandbox Code Playgroud)
仅导入类型:
import type { SomeType } from '@my-app/lib1';
import { someFunction, SomeClassToInstantiate } from '@my-app/lib1';
import type { OtherType } from '@my-app/lib2';
import { otherFunction, OtherClassToInstantiate } from '@my-app/lib2';
Run Code Online (Sandbox Code Playgroud)
基本上,我的很多导入都会重复,并且很难跟踪我是否以正确的方式导入所有内容(如果我导入为我在文件中实际使用的类型,则编译器会进行标记 - 但相反的情况则不然)等等,我没有找到任何工具来标记导入仅用作类型并且应该切换到仅类型导入)。
也许我更注意到这个问题,因为我正在使用 NX 并且我的应用程序的很多代码都来自库的桶文件;即,经常发生类型和非类型都必须从同一模块导入的情况。
所以我想知道,在任何地方使用仅类型导入能获得什么实际优势?是否在某些特定情况下使用它肯定有帮助,并且在所有其他情况下都可以绕过它?
如果我上一个问题的答案是应该在可能的情况下始终使用它,那么您是否知道在可以使用类型导入时强制执行类型导入的 linting 规则?
我不喜欢导入量比以前增加近一倍的混乱,甚至不知道它们始终在各地的常规/仅类型导入中划分。
给定一个类型化的泛型函数,我想为该函数创建一个泛型别名,但似乎我不能。换句话说,这不起作用:
// foo.tsx
function foo<T>(arg: T): T {
return arg
}
type FooT = typeof foo // works, but alias isn't generic: <T>(arg: T) => T
type FooParametersT = Parameters<typeof foo> // sure: [unknown]
type FooReturnT = ReturnType<typeof foo> // no problem: unknown
type GFooT<T,> = typeof foo<T,> // yikes
type GFooParametersT<T,> = Parameters<typeof foo<T,>> // nope
type GFooReturnT<T,> = ReturnType<typeof foo<T,>> // fails
Run Code Online (Sandbox Code Playgroud)
我真正想做的是在别人的库中获取函数的类型,然后围绕它构建一个接口。例如:
import { useState } from "react"
type UseStateFnT<T,> = typeof useState<T>
const wrapUseState: (toWrap: UseStateFnT<T,>) …Run Code Online (Sandbox Code Playgroud) 我收到此错误:
“参数 (number[]) 与 SpreadsheetApp.Range.setValues 的方法签名不匹配。”
当我尝试将一组值写入工作表时,在我的 Google Apps 脚本中。
下面是代码的缩短(简化)版本。实际代码运行了大约 10,000 条记录。
当setValues调用时,错误发生在最后一行。
我知道我在这里错过了一些非常简单的东西。
function writeArrayToSheet() {
var ss = SpreadsheetApp.openById("Spreadsheet_ID");
var orderSheet = ss.getSheetByName("Sheet_Name");
var vTable = orderSheet.getRange(1,6,5,11).getValues(); //Raw data
var vWriteTable = []; //Data that will be written to sheet
var updateTime = new Date();
var i = 0;
var vSeconds = 0;
while (i < 5 && vTable[i][0] != "") {
//Logic section that calculated the number of seconds between
if (vSeconds == 0) …Run Code Online (Sandbox Code Playgroud) 我在Flutter 中有这个项目,但由于 Gradle 版本,我已经有几个星期无法构建 apk。我已经尝试了所有方法,但 Flutter 总是返回以下错误:
我已经安装了我发现的每个更新,尽管它显示 Gradle 版本是 4.10.2。
...
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\israel.gomes\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\audioplayers-0.17.4\android\build.gradle' line: 25
* What went wrong:
A problem occurred evaluating root project 'audioplayers'.
> Failed to apply plugin [id 'kotlin-android']
> The current Gradle version 4.10.2 is not compatible with the Kotlin Gradle plugin. Please use Gradle 5.3 or newer, or the previous version of the Kotlin plugin.
* Try:
Run with --stacktrace …Run Code Online (Sandbox Code Playgroud) 我有一个 Google Apps 脚本自定义函数,非常简单:
function sharesBurned(sharesToSell) {
return "tree"
}
Run Code Online (Sandbox Code Playgroud)
显然,当我在 Apps 脚本中测试它时,这是有效的。例如,在我的工作表中,当我在单元格中调用它时sharesBurned(2),它有时会返回“正在加载”,并且在单元格的右上角会出现一个红色三角形符号,当将鼠标悬停在该符号上时,会显示:“错误 -加载数据中...”。
我知道有几个关于此的线程,但它们已经很旧了,而且我已经尝试了所有建议,但都无济于事,即:
需要明确的是,该函数有一半的时间无缝地加载到单元格中,而另一半则像这样卡住了。我非常怀疑这是脚本本身的问题,所以这可能是 Google Sheets 的一些奇怪的错误(缓存?没有线索)。
我有一个深度嵌套的数据结构,我希望能够引用其中的内部类型,但该类型没有自己的名称/定义。例如:
MyQuery['system']['errors']['list'][number]
Run Code Online (Sandbox Code Playgroud)
MyQuery我使用 graphql-codegen 从 graphql 查询自动生成类型。我想要 single 的类型error,但有两个问题:
我尝试了以下方法:
type Error = NonNullable<NonNullable<NonNullable<MyQuery>['system']>['errors']>['list'][number]
Run Code Online (Sandbox Code Playgroud)
?.['field']也不起作用)type Error = MyQuery?['system']?['errors']?['list']?[number]
Run Code Online (Sandbox Code Playgroud)
const error = queryResult?.system?.errors?.list?.[0]
type Error: typeof error
Run Code Online (Sandbox Code Playgroud)
import { DeepNonNullable } from 'utility-types'
type Error = DeepNonNullable<MyQuery>['system']['errors']['list'][number]
Run Code Online (Sandbox Code Playgroud)
基本上我要问的是是否有一种更简单的方法可以在打字稿中进行“类型的可选链接”。我的 API 很容易出现空值,如果我能比使用多个 API 更容易地做到这一点,那将会非常有用NonNullable<T>
noFallthroughCasesInSwitch我已在 tsconfig.json 文件中启用该选项。function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
switch(getRandomInt(3)) {
/* falls through */
/* fall through */
/* FALLTHROUGH */
case 1: /* falls through */ /* fall through */ /* FALLTHROUGH */ /* <----- Still getting an error here "Fallthrough case in switch. (7029)" */
/* falls through */
/* fall through */
/* FALLTHROUGH */
console.log(1);
/* falls through */
/* fall through */
/* FALLTHROUGH …Run Code Online (Sandbox Code Playgroud) typescript ×7
arrays ×1
async-await ×1
eslint ×1
flutter ×1
generics ×1
gradle ×1
lint ×1
nested-types ×1
node.js ×1
tsc ×1