我正在使用typescript,遇到了一些问题。最简单的演示是:
type g = 1 & 2 // never
type h = ((x: 1) => 0) & ((x: 2) => 0) // why h not never
type i = ((x: 1 & 2) => 0)// why x not never
我不明白为什么类型h不是never,类型i中的参数x也不是never
type e = (((x: 1) => 0) & ((x: 2) => 0)) extends (x: infer L) => 0 ? L
描述
我正在重写<< operator以使std::ostream易于在代码中显示对象。在定义要显示的对象类型时,我使用不同的命名空间。
这导致了编译错误。我找到解决办法了。似乎必须在全球范围内声明压倒一切,但我真的不明白为什么。有人能解释一下是什么原因造成的错误吗?
错误
main.cpp:22:38: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘std::vector’)
std::cout <<"printVecto
我正在阅读Typescript手册,我很难理解为什么下面的代码片段有错误消息:
function fn(x: string): void;
function fn(vo) {
// ...
}
// Expected to be able to call with zero arguments
fn();
这是解释,但我不明白,谁能给我解释一下这是怎么回事?
从外部看不到实现的签名。在编写重载函数时,应该始终在该函数的实现之上具有两个或更多签名。
在研究EnumSet<E> of方法时,我看到了of方法的多个重载实现:
public static <E extends Enum<E>> EnumSet<E> of(E e)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
.
.
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5)
然后,使用
如果这是一个非常基本的问题,我很抱歉。我在自学TypeScript,根据我的教程,交叉点中类型的顺序应该无关紧要,例如A & B和B & A应该是同一类型。
但我偶然发现了这种边缘情况,其中类型的顺序确实很重要,尽管我不明白为什么。
abstract class Test
{
constructor(a: string, b: boolean)
{ }
}
type A = typeof Test;
type B = new (...args: unknown[]) => unknown;
type AB = ConstructorParameters
考虑到以下代码:
type Foo = {
foo: number
}
type Bar = {
bar: string
}
type FooOrBar =
| Foo
| Bar
type FooOrBarList =
| Foo[]
| Bar[]
function processFooOrBar(x: Foo): Foo;
function processFooOrBar(x: Bar): Bar;
function processFooOrBar(x: FooOrBar): FooOrBar {
return x
}
给这个:
Class<? extends Enum> enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like
未检查的未检查方法调用: valueOf(java.lang.Class,java.lang.String)在java.lang.Enum中应用于(java.lang.Class,java.lang.String)
我不想使用泛型,因为这是一个重大的变化
假设我有这部分代码:
#include<iostream>
using namespace std;
class A {
public:
virtual int f(const A& other) const { return 1; }
};
class B : public A {
public:
int f(const A& other) const { return 2; }
virtual int f(const B& other) const { return 3; }
};
void go(const A& a,
我想开发一个非常小的脚本来读取一个句子,并在控制台中生成一个n克图形的输出。
这是一个例子
Example
"Show me the code."
Returns
[
"Show",
"Show me",
"Show me the",
"Show me the code",
"me",
"me the",
"me the code",
"the",
"the code",
"code
我不明白为什么下面的代码混淆了编译器。
enum Enum {
a,
b,
c
}
const func = <T extends Enum>(param: T = Enum.a) => param;
这里,我将T限制为Enum类型,并将默认参数设置为枚举器Enum.a。但是TypeScript在带有消息Type 'Enum.a' is not assignable to type 'T'.的(param: T = Enum.a)部件上失败
我在这里漏掉了什么,因为我自己找不到它,我只能找到基本的在线资源?
这是一个
让我们使用通配符来研究一些一般的实例化情况:
1
这段代码
List<?> list = new ArrayList<?>();
生成以下错误:
required: class or interface without bounds
found: ?
2
但这是
List<?> list = new ArrayList< Set<?> >();
编译成功。
3.
这是:
List<Set<?>> list = new ArrayList< Set<?> >();
编译也是成功的。
我试图使用用TypeScript编写Node.js服务器,但在将fs.readFile转换为rxjs形式时遇到了错误。我希望下面的代码可以在TypeScript中工作
// This is a JavaScript example from the official documentation. It should
// also work at the TypeScript envrionment.
import * as fs from 'fs';
import { Observable } from 'rxjs';
let readFileAsObse
我正在努力学习TypeScript,并一直在跟踪在线教程示例,以获得TypeScript中的枚举支持。对于下面的代码片段:
enum daysoftheweek{
SUN, MON, TUE, WED, THU, FRI, SAT
}
let day:daysoftheweek ;
day = daysoftheweek.FRI; //line 7
if (day === daysoftheweek.MON){
console.log("got to go to work early");
}else{
console.log("I may
我无法解释为什么第二个调用(B)没有提供任何错误,因为有两个char元素,并且这个调用没有特定的匹配。
为什么它被称为第二个(**2.**),,而不是第一个(1.)版本?
我注意到有一些自动转换。我不明白的是,为什么'a'被提升为int,而'c'却没有。
// 1.
int fun(int a, int b)
{
return a + b;
}
// 2.
int fun(int a, char b)
{
return b - a;
}
// 3
int fun(float a, float b)
{
return a * b;
}
我有两个枚举映射,希望为枚举键创建一个数组,删除数组中的公共子字符串,这样我也可以将其用于其他枚举映射。枚举格式为 enum Test {
test_value_error = 1;
test_value_log = 2;
test_value_main = 3;
test_value_acknowledge = 4;} enum Test1 {
short_set_measure = 5;
short_set_log_check = 6;
short_set_finite_check = 7;}
in this case I want to re
我不明白这里发生了什么
class A{};
class B : A {};
void func(A&, bool){}
void func(B&, double){}
int main(void)
{
B b;
A a;
bool bo;
double d;
func(b, bo);
}
编译时,Visual2010在func(b, bo);行上显示以下错误
2 overloads have similar conversions
could be 'void func(B &,double)'
or