我有一个对象,其中一个属性的类型依赖于另一个属性的类型。例如: // The item types
enum ItemType { APPLE, SANDWICH }
// Map of a given item type to it's options
type ItemTypeToOptionsMap = {
[ItemType.APPLE]: { ripeness: string }
[ItemType.SANDWICH: { filling: string }
}
// The item type in question
type Item<T extend
假设我有一个映射到数据库表的类型,其中键是列名:
interface MyTable {
foo: string;
bar: number;
}
然后,我有另一个类型,它从表单字段映射到表行。
interface MyTableMap<T> {
columnName: keyof T;
formFieldName: string;
value: any;
}
// I want this to generate an error because 12345 is not a string based on the column name of "foo
TypeScript专家!
我正在编写一个灵活的TypeScript函数,它接受这两种类型的参数:A类和B类是独立的,而不是继承的。但是,我需要否认使用联合类型A | B调用的函数的模糊用法。有没有办法在TypeScript中声明这一点?注意:该函数必须是一个非常单一的函数,但对于每次使用都不能分离函数。
class A {/* snip */}
class B {/* snip */}
let a:A = new A();
fn(a); // definitive usage is OK
let b:B = new B();
fn(b); // definitive usage is O
我有一个这样的界面:
interface A {
a: number;
b?: string;
c?: string;
}
我想让下面的代码正常工作:
// obj: A
// this should not throw errors
obj.b && obj.c
// this should throw errors since I want to make c mandatory when b is passed
let obj2: A = { a: 1, b: "test" }
我试着这样做:
type A = ({ b: str
我有一个可以返回数字、Error或NegativeError的函数
class NegativeError extends Error {
name = 'NegativeError'
}
function doThing (a: number): number | NegativeError | Error {
return a < 0 ? new NegativeError() : a === 0 ? new Error() : a
}
const done = doThing() // T | NegativeError | Error
然而,当我删除签名
运行以下代码:
type Test =
| L of int
| M of string
| N of bool
let client = new MongoClient("mongodb://localhost:27017")
let _db = client.GetDatabase("TestDB")
let collection = _db.GetCollection<Test>("TestLog")
collection.InsertOne(L 22)
我将以下内容插入到我的集合中:
_id: ObjectI
我如何才能做到这一点?
type Fruit = "apple" | 'banana' | 'coconut'
type FruitCollection = { [f in Fruit]?: number }
const validFruitCollection: FruitCollection = { apple: 1, coconut: 2 }
const emptyCollectionShouldNotPass: FruitCollection = {} // I don't want typescript to let t
由于可以使用VueJS v-bind:style绑定设置CSS变量,因此我正在尝试创建一个联合类型,其中传递给v-bind:style的对象保留了CssStyleDelcaration的类型,但可以轻松地接受任意属性名称:
type Dictionary<T> = { [key: string]: T };
type Nullable<T> = T | null;
type CssStyleObject =
Partial<CSSStyleDeclaration> |
Dictionary<Nullable<string>>;
在Visual中,我创建一个默认的TypeScript站点。
然后我使用Nuget添加jquery.d.ts。
然后我编译了这个应用程序。
错误窗口显示jquery.d.ts文件中的101个错误,如:
Error 341 ',' expected.
如何解决这些错误?
我是否错误地导入了文件,或者是否存在需要设置的设置?
UI中显示的实际错误如下:
A parameterized initializer is only allowed in a function or constructor implementation.
在这条线上:
replaceAll(target:
我试图理解TypeScript 2.8中引入的条件类型,并阅读相应的文档。
下面的示例可以在下面的链接中看到
本节有一个例子:
分布条件类型
条件类型在与映射类型组合时特别有用:
type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T];
我理解{ [K in keyof T]: T[K] extends Function ? K : never }部分,它选择所有类型函数的键。但是我不明白为什么在[keyof T]末尾有一种
为什么我不能仅仅根据成员x是字符串而不是数字的信息来区分这个联合呢?为什么我必须使用文本类型?
type A = { x: string ; y: string }
type B = { x: number ; y: number }
type C = A | B
function fn(p: C) {
if (typeof p.x === 'string') {
// Typescript is unable to infer p as A
// Typescript infer p.y as (string | number), why not ju
我在TypeScript中定义了以下类型:
type ComponentProps = Partial<{
title: string;
children: string[];
classes: string[];
// Maximum two status flags can be provided
statusTypes: [int, int];
}>;
如何在F#中正确表示此Partial<T>类型?我知道Option类型,但是有些类型太大了,使用Option感觉不太地道。
有没有一种方法(类似于函数式语言中的模式匹配)来分解TypeScript中的联合类型,也就是像这样的构造:
var a: Foo | Bar = ...;
a match {
case f: Foo => //it's a Foo!
case b: Bar => //it's a Bar!
}
如果没有这样的构造--创建这样的构造有什么技术上的困难吗?