我有以下类型:
export type a = {
todo: string;
};
export type b = {
id: number;
};
export type TodosAction = Action<string> & (a | b);
我有一个功能:
function doSmth(action:TodosAction){
switch(action.type){
case "a":
action.todo //TS error, doesn't exist
(action as a
我有一个type定义,它是从一个不同的npm包导入的,它包含100多个联合字符串,如下所示:
type SomeType = "a" | "b" | "c" \\ | ... and it goes and goes
我想签入我的代码,如果字符串变量属于这个SomeType。是否有一种不需要我编写助手函数()列出所有可能的值()的方法?
在我的例子中,列出所有的值可能不是最好的,因为来自npm包的类型值在将来可能会改变,并且因为会有大量的值。我在寻找更聪明的方法,如果它存在的话。
我意外地将以下代码添加到我的TypeScript程序中,意思是键入let readIndex=0
let readIndex:0;
由此,TypeScript编译器生成以下JS代码:
let readIndex;
为什么编译不失败?我不认为这是一个有效的语法,因为0不是一个有效的类型名称,但显然我错了。
那么,这个语法意味着什么呢?
我希望下面的代码能够正常工作,但是第7行出现错误,消息为Type '"one"' is not assignable to type '"two"'. type One = { type: 'one', value: number }
type Two = { type: 'two', value: number }
type OneOrTwo = One | Two
function handleOneOrTwo(oneOrTwo: OneOrTwo): OneOrTwo {
const
这个项目是教育用途,我非常清楚,优秀的编译器已经存在。
我目前正在努力通过著名的,并刚刚开始实现我自己的雷克萨斯。令人惊讶的是,除了文字之外,它工作得很好。我不明白如何使用符号(查找)表来处理文字,这本书似乎没有很好地涵盖:
在下面的代码中,60是一个数字文字:
int myIdentifier = 60;
龙书上写着:
从技术上讲,对于词汇60,我们应该构造一个类似于(数字,4)的标记,其中整数60的内部表示的符号表中有4个点.
理解-我创建了以下令牌:
<enum TokenType, int lookupIndex>
//TokenType could be
是否可以在不首先声明/定义数组的情况下将数组发送到C函数?
对于整数来说,这是可能的。
int add(int a, int b) {
return (a + b);
}
void main(void) {
int c;
int a=1, b=2;
/* With declaring (works fine)*/
c = add(a, b);
/* Without declaring (works fine)*/
c = add(1, 2);
}
数组中是否有类似的内容?
#include <stdio.h>
void
正如您在下面的代码中所看到的,TS编译器将类型推断为:
const message = Math.random() > 0.5 ? "hello, can you here me" : null;
因此,消息变量被推断为"hello, can you here me" | null,这是有意义的,因为有两个绝对可能的值,但是在下面的示例中,返回值仍然是一个绝对值,但是getName类型是string基元类型,而不是John Gump。
const getName = () => "John Gump";
我试图创建一个使用"scrollIntoView“的方法,并可选择地设置它的参数,但是当尝试设置block参数时,它说:
Type 'string' is not assignable to type 'ScrollLogicalPosition | undefined'.
当尝试设置behaviour时,会给出:
Type 'string' is not assignable to type 'ScrollBehavior | undefined'
这是代码:
scrollToFirstElementOfClassNam
我想在javascript中完成sql 'like‘的所有操作。例如:
var str="South-Asia, North-America, South-Africa";
var firstwordmatch=South%; //will result South-Asia,South-Africa
var last match=%ica // will match North-America, South-Africa
我尝试了以下几点:
var ex = str.split('%')[1]; // this for matching %ica
如果我键入以下内容:
interface A {
x: number
}
interface B {
y: number
}
type Z = A | B;
// here it allows me to create a variable of type Z with both members of type A and B.
let z: Z = {
x: 5,
y: 6,
}
我无法确保Z类型的对象包含A的所有成员,但不包含B的成员(或相反的)。有可能用TypeScript吗?经过大量的研究,我倾向于“不”的答案,但我不确定。
我正在尝试编写一个函数,它将接受一个字符串文本,并返回一个带有一个字段的对象,该字段的名称就是该字符串文本。我可以编写一个函数来做我想做的事情,但是我不知道如何表达它的参数类型是字符串文本的约束。
我最近得到的是使用泛型类型,它扩展了string。这允许字符串文本类型,但也允许字符串文本类型和类型string的联合,我不希望将它们传递给函数。
只要K是字符串文本类型,这就编译并执行我想做的事情。注意,类型断言在类型记录3.4中不是必需的,但在3.5中是必需的。
function makeObject<K extends string>(key: K): { [P in K]: st
我有一个来自库的外部类型:
type Anything = Record<string, any>;
我想重写它,不允许使用特定的密钥c。
我希望取得的成果:
type CustomAnything = Anything & { c?: never } // This is what I was able to come to, but it does not completely forbid the usage of the `c`. Undefined is allowed in this case
const a: CustomAnything = { a: 1
我们如何编写一个预处理器宏来替换的每个实例:
func(
使用
func((DemoUnion)?
此外,还可以使用宏用func((DemoUnion)替换func((DemoUnion)(DemoUnion)
顺便说一下,下面是一个如何定义DemoUnion的示例:
union DemoUnion {
char c;
short s;
int i;
float f;
}; typedef union DemoUnion DemoUnion;
// the typedef allows us to declare instances by writing