问题标签 [typescript3.0]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
typescript - 如何在打字稿中为数字枚举提供替代字符串定义
我有一个如下所示的数字枚举。
当我访问下面的枚举字符串时,我需要获取枚举的替代字符串值。
上面的行应该给我“一个评级”而不是一个。
我需要坚持使用数字枚举并且不能使用字符串枚举。一旦解决方案我能想到的是使用下面的字符串数组。
但是有没有更好的方法在打字稿中实现这一点?
我期待像下面这样的枚举在 c# 中的描述属性
angular - 数组中的差异.filter 签名和用法
我遇到的问题是使用Array<T>.filter
和接口定义之间的差异。
我在 Angular2 组件中实现了这个过滤器:
奇怪的是当我进入接口定义时:
我在用着
我提供的回调函数其实是:
但是界面显示了 2 个其他参数 - 索引和数组作为非可选参数。然而,这一切都可以编译和工作。我认为可选参数意味着
就未记录行为的打字稿编译器而言,是否还有其他事情发生?
typescript - 如何为打字稿类的抽象方法定义模板?
当我们在 TypeScript 中创建一个抽象类时,它就像:
...然后当我们用它扩展一些类时:
创建它throw new error('Method not implemented!');
是为了帮助我们不要忘记实现它的代码。我不知道是 VSCode 做到了还是它是预定义的 TypeScript 东西。
有谁知道如何将此行更改为其他代码。如果我们可以为每个抽象方法定义一个模板,告诉下一个程序员或多或少应该将什么编码到方法中,而不是仅仅依赖于注释,那将是非常好的......
visual-studio-code - angular 7 array push() 方法在 Visual Studio 代码中显示错误警告但运行正常
我最近将我的项目从 angular6 升级到了 angular7。Visual Studio 代码开始在推送、长度等方面显示警告。例如, [ts] 属性“推送”在类型“{}”上不存在。
我已经初始化了数组
同样,它的长度也显示
[ts] 类型“字符串”上不存在属性“长度”。
但是当它运行时
服务
它运行正常。有什么建议么
typescript - TypeScript: generically infer union type member based on a string literal property
TypeScript (v3.2.2) allows me to define a union of interfaces, each with a unique string literal property which can be used as a type guard, e.g.
I want to write a function isDeviceType
in a generic way:
However, the way I have defined the DeviceOf
type is pretty verbose since it lists every single type within the union:
Is there a more concise way to define DeviceOf
? I have tried these:
My impression is that error TS1338 is the limiting factor, and so it's impossible to define DeviceOf
in a generic way in the current version of TypeScript.
typescript - 打字稿:使用“枚举”值作为“类型”
我想使用以下enum
的值:
如下所示:
通过使用类型提取/定义,例如:
更新的问题:
这是我的代码:
我的问题是,我怎样才能使用单个实体enum EGenderFormats
或同时使用interface IGenderFormats
两者?
我正在使用打字稿 3.2.2
谢谢
typescript - 如何从“.d.ts”文件的命名空间中的其他库导入类型或接口?
我正在制作工作中使用 AWS SDK 的 TypeScript 库,.d.ts 文件中有错误。
'MyLib' only refers to a type, but is being used as a namespace here. ts(2702)
我正在使用 typescript@3.2.4,分配types/
给 typeRoots at tsconfig.json
。我尝试import
进入命名空间定义,但没有解决。
types/src/lib/mylib.d.ts
src/lib/mylib.ts
我预计该代码不会出错。
javascript - 打字稿神秘的十字路口
TLDR:游乐场再现
在我的应用程序中,我定义了多个表单模块,大致如下:
如您所见,每个突变函数都返回一个期望特定形状variables
字段的函数。
每个模块的使用直接工作得很好:
但是,我还创建了这些表单的中央注册表,以便我可以从其他地方查找它们,如下所示:
这就是问题所在。当我尝试引用组合表单对象的单个成员时,Typescript 似乎会自动创建variables
字段的交集并引发以下错误:
我想我在这里遗漏了一些相当简单的东西,但希望能够深入了解如何获得理想的行为(当我专门检索 时firstModule
,我只希望它验证该模块中的 variables 字段)。如果我可以提供任何其他信息,请告诉我。
谢谢!
typescript - 访问键入的快速响应
我正在尝试express
Response
像这样扩展类型:
但我得到这个打字稿错误:
Property 'response' does not exist on type 'Application'.
我必须使用哪种类型而不是Application
?
typescript - tsc 在 dist 中创建 src 文件夹
我有这个文件夹结构:
这是我的tsconfig.json
:
在此文件夹结构中调用./node_modules/.bin/tsc
结果:
为什么src
包含在dist
?