假设,如果我有以下函数: function nullOrString(): string | null {
return null;
} 这不会产生任何错误: const value = nullOrString();
if (value) {
const foo: string = value;
} 但是下面的代码确实会产生错误Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'strin
使用类型记录3.0+。请参见以下涉及泛型类型变量的简单设置:
abstract class BaseClass {
public abstract merge<T>(model?: T): T;
}
class MyClass extends BaseClass {
public Value: string;
public merge<MyClass>(model?: MyClass): MyClass {
this.Value += model.Value; // <--Property 'Value' does not
我在我的项目中遇到了这个问题,下面是可以重现它的最小代码。我想知道是TypeScript的bug,还是我用错了。(实际代码比较复杂,但错误是一样的) 错误是:Type 'T' is not assignable to type 'T extends T ? T : T'. 我在TypeScript 3.0.3和3.3.1上测试 function test<T>(arg: T) {
let x: T extends T ? T : T;
x = arg;
}
以以下TypeScript代码为例:
const a: 0 | 1 = 1
if (a === 0) {
console.log('a is 0')
}
if语句将生成以下错误:
This condition will always return 'false' since the types '1' and '0' have no overlap.
(您可以在TypeScript游乐场中使用下面的示例:)
我的问题是,为什么这会导致错误?如果您悬停在a类型上,则它被正确地键入为const a: 0 | 1。如果编译器如
我想写一个函数来做三件事:
在通用类型T上操作
接受一个键:T的K,其中TK必须是布尔值
为传统知识赋值
我关注的是,它暗示了这种可能性:
type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T];
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
type NonFunctionPropertyNames<T&
假设我们有这样的代码:
class MyEvent {
timestamp:number;
}
class AEvent extends MyEvent{
x:number;
y:number;
}
class BEvent extends MyEvent{
key:string;
}
var fn:(event:MyEvent)=>void;
function AE(event:AEvent){
let x = event.x; //Ok, but BEvent hasn't 'x' property.
}
fn
我有一个接受其参数的函数,并返回一个可以使用这些参数的函数。不幸的是,我根本无法让TypeScript对其进行类型检查。下面是我的问题的一个简化示例:
type NoiseMaker<T extends Animal> = (animal: T) => void;
class Dog {
bark() {
console.log('Woof! Woof!');
}
}
class Cat {
meow() {
console.log('Meow')
}
}
type An
在typescript 2.5.2中,以下代码无法使用strictNullChecks进行编译
function ifnull<T>(x: T | null | undefined, def: T): T
{
return x != null ? x : def;
}
console.log(ifnull(2, 1));
给出错误:
Argument of type '2' is not assignable to parameter of type '1 | null | undefined'.
它似乎推断T是1而不是number。当不使
我有一个函数URLBuilder,它作为一个模块工作(它返回一个包含各种函数的对象,这些函数与上下文对象绑定得很好,所以this绑定按预期工作,我们可以在这些函数中正确地访问this ) 我的问题是为什么typescript不理解URLBuilder的返回对象中函数的this绑定?尽管我尝试手动添加一个返回类型,但Typescript抱怨when using the URLBuilder在没有this上下文的情况下无法接受函数。 我们如何让typescript在返回的对象中理解这个绑定函数的this? 我要解决的Typescript错误是: The 'this' contex
通过阅读和,我了解了TypeScript中的条件类型,我想我在TypeScript中发现了一个长期存在的错误。
例如,这段代码生成类型({ value: string } | { value: number })[],而我期望它生成类型({ value: string | number })[]
// Given an array, make a new array that boxes up string or
// number elements into a new object with a 'value' property
declare function boxup
简单的代码示例:
type Foo = { foo: string };
type FooBar = { foo: string; bar: number };
let a: Foo;
// Doesn't work
a = { foo: 'x', bar: 1 };
/*
Type '{ foo: string; bar: number; }' is not assignable to type 'Foo'.
Object literal may only specify known properties, and '
在forEach回调函数中设置变量时,该变量的类型似乎不正确。一个简单的例子:
let foo: (string | null) = null;
[1,2,3].forEach((i) => {
foo = "bar";
});
if(foo == null) {
throw new Error("not found")
}
# Typescript complains that `length` is not a property of type `never`
console.log(foo.length)
在forEach之后,我