自从TypeScript引入了联合类型以来,我想知道是否有任何理由声明枚举类型.请考虑以下枚举类型声明:
enum X { A, B, C }
var x:X = X.A;
Run Code Online (Sandbox Code Playgroud)
和类似的联合类型声明:
type X: "A" | "B" | "C"
var x:X = "A";
Run Code Online (Sandbox Code Playgroud)
如果它们基本上服务于同一目的,并且工会更强大和更具表现力,那么为什么需要枚举?
我有类似这样的HTML:
<div class="table-responsive">
<table class="table borderless">
<caption>
<h3>Announcements</h3>
</caption>
<tbody>
<tr >
<td>
If you are waiting for your certificate from your trainer, please contact them. Our turnaround time is between 1-2 months from the time we receive your details from your trainer, which we expect to be at the start of your program. The remainder is dependent upon how quickly your trainer has send in all the required paperwork and made payment, etc.
</td>
</tr>
</tbody>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
当我在一个小视口中查看输出时,表格会正确调整大小,但表格单元格中的段落内容不会被包装,因此会显示滚动条.我预计响应行为会包含段落内容.我该如何实现这一目标?
html-table responsive-design twitter-bootstrap twitter-bootstrap-3
我一直在使用TypeScript和本机Promise在SPA中使用该功能,我注意到即使我将长时间运行的函数重构为返回promise的异步函数,UI仍然没有响应.
所以我的问题是:
新的异步/等待功能究竟如何帮助避免阻止浏览器中的UI?在使用async/await实际获得响应式UI时,是否需要采取任何特殊的额外步骤?
有人可以创建一个小提琴来演示async/await如何帮助使UI响应?
async/await如何与先前的异步功能(如setTimeout和XmlHttpRequest)相关联?
javascript user-interface asynchronous async-await ecmascript-2017
几天前安装的最新版VS代码(2017年10月更新)不断在.ts代码文件的顶部添加各种奇怪的import语句.
例如,当我编辑文件时,VsCode突然在文件顶部添加了这样的一行:
import { Stack } from "../../../../../../../../../Repos/Web/node_modules/@types/d3";
Run Code Online (Sandbox Code Playgroud)
如何禁用此行为?
我有一个像这样的对象:
var obj = {
key1: "apple",
key2: true,
key3: 123,
.
.
.
key{n}: ...
}
Run Code Online (Sandbox Code Playgroud)
因此obj可以包含任意数量的命名键,但值必须都是string,bool或number.
如何obj在TypeScript中声明接口的类型?我可以声明一个联合类型或类似的关联数组(或可变参数元组)吗?
在自托管运行器计算机上运行 Github 操作时,如何在我的 Github 操作 .yaml 脚本中访问已在计算机上设置的现有自定义环境变量?
我已设置这些变量并重新启动了运行程序虚拟机多次,但无法使用脚本中的 $VAR 语法访问它们。
continuous-integration environment-variables github-actions github-actions-self-hosted-runners
我想将一个对象类型映射到一个子类型,该子类型只包含值属于特定类型的键。
例如,像ExtractNumeric<T>, where
ExtractNumeric<{ str: string, num: number }>应该等价于类型:{ num: number }
我试过这个,但它不起作用:
type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never }
此代码段引发类型错误:
let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 }
因为虽然str键期望值为never,但编译器会抱怨它的缺失。
我知道IEnumerable.ToList()应该创建一个新的List,但是项目指向IEnumerable中的相同原始项,如ToList()所述 - 它是否创建一个新列表?
但是,我使用VS 2012的代码得到了一些奇怪的行为; WPF; 和.NET 4.0.它开始于IEnumerable.SequenceEquals()似乎没有像我预期的那样工作.我在我的QuickWatch对话框中挖掘,令人难以置信的是,以下语句的计算结果为false:
this.Items.First () == this.Items.ToList ()[ 0 ]
Run Code Online (Sandbox Code Playgroud)
我甚至尝试过:
this.Items.ToList ().IndexOf(this.Items.First ())
Run Code Online (Sandbox Code Playgroud)
评价为-1.
Items 在WPF自定义控件上声明为属性,如下所示:
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register (
"Items",
typeof ( IEnumerable<UserLayoutType> ),
typeof ( UserLayoutSelectorControl ),
new FrameworkPropertyMetadata ( null, FrameworkPropertyMetadataOptions.AffectsRender, UserLayoutSelectorControl.PropertyChanged ) );
public IEnumerable<UserLayoutType> Items
{
get
{
return ( IEnumerable<UserLayoutType> ) this.GetValue ( UserLayoutSelectorControl.ItemsProperty );
}
set
{
this.SetValue ( UserLayoutSelectorControl.ItemsProperty, value );
}
}
Run Code Online (Sandbox Code Playgroud)
UserLayoutType只是XSD工具生成的类,具有以下声明:
//
// This source code was auto-generated …Run Code Online (Sandbox Code Playgroud) 考虑以下:
type UnwrapNullable1 = Required<{ x?: (1 | undefined) }>["x"]
type UnwrapNullable<T> = Required<{ x?: T }>["x"]
type Test = UnwrapNullable<1 | undefined>
Run Code Online (Sandbox Code Playgroud)
类型UnwrapNullable1被正确推断为1,但Test应该等同于UnwrapNullable1被推断为1 | undefined。
这是打字稿的限制还是某种错误?
我在我的打字文件中有这个:
declare namespace Somatic {
enum PropType {
html,
object,
css
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个文件index.ts中,我有一个较短的别名,因为:
type PropType = Somatic.PropType;
Run Code Online (Sandbox Code Playgroud)
然后我想在switch语句中使用别名枚举类型:
switch (propType) {
case PropType.html:
break;
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
但是Typescript无法识别别名的枚举类型值.这有什么不对?
typescript ×6
javascript ×3
enums ×2
unions ×2
.net-4.0 ×1
alias ×1
async-await ×1
asynchronous ×1
c# ×1
generics ×1
github-actions-self-hosted-runners ×1
html-table ×1
ienumerable ×1
list ×1
mapped-types ×1
tuples ×1
typing ×1
union-types ×1