我偶然发现了TypeScript类型的问题,当有条件的泛型出现时,我不能将更窄的类型分配给更广泛的类型。但是,所有泛型参数都是完全实例化的,因此TypeScript应该能够完全解析这两种类型,并根据每个属性的类型对它们进行比较。
这是我试图做的抛出错误的简化版本:
// Returns keyes that have type extending Types
type KeysMatching<T, Types> = { [K in keyof T]: T[K] extends Types ? K : never }[keyof T];
type Mapping = {
我目前有一个iOS应用程序的问题,我正在开发本机脚本和角质。最近,我将Xcode更新为9个版本,并在Xcode中为我的项目执行了一个清理工作,然后发生了问题,现在当我尝试执行tns运行iOS时,这里是输出:
tns run ios
Searching for devices...
Executing before-liveSync hook from /Users/onols/Desktop/ClubiesApp/hooks/before-liveSync/nativescript-angular-sync.js
Executing before-prepare hook from /
在使用Typescript时,我总是想知道为什么不能自动推断出这两种情况?有没有逻辑上的原因?你将如何克服这一点?
type Test1<T> = {
a: T,
b: T extends string ? object : number,
};
// Error: Generic type 'Test1' requires 1 type argument(s).
// I expect Typescipt to infer T from the `a` property
const test1: Test1 = {
a: "test1"
在下面的代码中,我尝试创建一个访问给定输入的data属性的类型安全函数。我的目标是避免在任何地方重复对data的访问,为此,我希望将样板保持在最小(例如,我希望TypeScript推断尽可能多的类型)。
interface User {
name: string
}
interface HttpResponse<T> {
data: T
status: number
}
type HttpPromise<T> = Promise<HttpResponse<T>>
function fetch<T>(url
为什么在TypeScript中不可能出现以下情况?
abstract class Generic<T> {
// ...
}
class Specific1 extends Generic<string> {
// ...
}
class Specific2 extends Generic<number> {
// ...
}
// this is where stuff goes wrong, compiler wants me to provide generic type arguments
//
显然,在typescript中,可选参数和未定义的联合类型的参数之间存在差异 function test(s: string|undefined) {}
function test2(s?: string) {}
test() // error: An argument for 's' was not provided
test2() // OK 我想声明一个泛型函数,根据提供的类型,它的参数是可选的或必需的。例如: // Intention: if type is the same as its Partial counterpart then the paramete
我们有一个泛型类型,它成功地从它接收的类型中提取数据属性。它是in the playground,它是这样工作的。 type DataPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
type FooBarBaz = {
foo: string;
bar: Function;
baz: number;
};
const f1 = () => {
type DataProps = DataPropertyNames<FooBarB
在用npm测试在visual代码中运行量角器测试时,出现了跟随错误,似乎是node_modules/@types/jasmine/index.d.ts的问题,如何消除这个错误?
C:\MyFiles\NewTechonologies\Protractor\TypeScript\Test>npm test
> example-typescript@1.0.0 pretest C:\MyFiles\NewTechonologies\Protractor\TypeScript\Test
> npm run tsc
> example-typescript@1.0.0 t
我有一个环境TypeScript模块表示支持任何承诺/A+库的库:
interface Test {
funcName():Promise<string>;
}
因此,我需要对其进行调整,使任何承诺库的协议都可以在声明级访问:
interface Test<P> {
funcName():P<string>;
}
但是TypeScript会立即抱怨:Type 'P' is not generic,甚至在我使用它之前。
请注意,我不能将自定义承诺库包含到与Test相同的文件中,因为我必须从另一个模块传递它。
如果我把代码改为:
inte
我是刚接触typescript的人,我正在尝试理解泛型是如何工作的。我想知道为什么下面的代码不能运行: function identity<T>(arg: T): T {
let num: number = 2;
return num;
}
let output = identity<number>(1); 我得到一个错误:类型'number‘不能赋值给类型'T’。如果函数的输入是number,这是否意味着number的返回类型也应该起作用,因为我们说的是T是number类型?
// Generic Constraints
class Car {
print() {
console.log('I am a car')
}
}
class House {
print() {
console.log('I am a house')
}
}
interface Printable {
print(): void;
}
// tell Typescript that I promise the T type will satisfy the Printable interface
function pri
假设泛型函数有两种类型,它们的返回类型不同。一个是返回类型T,另一个是T[]
type F1 = <T>(t: T) => T
type F2 = <T>(t: T) => T[]
我想知道如何将这两种类型合并成一种泛型类型。我想象的方法是让这个新泛型接受另一个泛型类型作为参数(比如C++的):
// Caution, Fantasy-TypeScript ahead!
// Accepts generic type R as parameter
// to transform return type of generic function
type F&
为什么我不能打电话给SomeGenericMethod<SomeGenericType<>>
class NotGeneric { }
class Generic<T> { }
class Program
{
static void Main(string[] args)
{
PrintType(typeof(NotGeneric));
PrintType(typeof(Generic<>));
PrintType<NotGeneric>();
Pri
我有一个泛型类Generic<T>和另一个泛型类,另一个泛型类是GenericWrap<U extends Generic<any>。它有一个Generic<any>作为成员。(请参阅以下代码) 现在,NumberGenericWrap使用泛型类型<NumberGeneric>扩展了GenericWrap。此时,我希望NumerGenericWrap.generic的类型为Generic<number>,这样NumberGenericWrap.getValue()的返回类型就可以为number。但它最终以any类型结束。 ty
我正在尝试写出一个具有附加功能的泛型类的具体案例,但我不确定为什么会发生以下情况。
假设我有一个泛型类:
class Generic<T>
{
protected T value;
}
如果我写出一个特定的实现,我实际上不能使用我缩小范围的特定类型:
编辑:我搞砸了,这不管用。
class Generic<float>
{
// This doesn't work
public void Add()
{
value + 1.0f;
}
}
但是如果我继承了特定的版本,它确实可以工作:
class Specif
我在TypeScript中使用了TypeScript约束,如下所示:
class Animal {}
class Lion extends Animal {}
class Bear extends Animal {}
class ZooKeeper<T extends Animal> {
constructor(p: T = new Animal()) {
}
}
new ZooKeeper(new Animal());
但是p: T = new Animal()包含一个错误:
“动物”类型不能指定为“T”。
构造器动物()
--为什么是,以及我要做什
在Typescript中,我有一个泛型接口,表示我想要传递给函数的类型。
//foo must be an object {}, not a number or string or boolean or array
interface MyInterface<T extends {[key: string]: any}> {
foo: T
}
所以我将我的函数设为泛型函数,但是TS没有从我的接口推断出约束
const myGenericFn: <T, U extends MyInterface<T>>(bar: U) => void = (bar)
我目前正在学习 --并试图使用TypeScript来更好地理解它们。
我想做以下几点:
type Bind<M, A, B> = (ma: M<A>) => (a_mb: ((a: A) => M<B>)) => M<B>
type Unit<M, A> = (a: A) => M<A>
但是我得到了以下TypeScript错误:
error TS2315: Type 'M' is not generic.
type Bind<M, A, B> = (ma: M<A
给定下列类型
protocol AProtocol {}
protocol AnotherProtocol {}
class AClass: AProtocol, AnotherProtocol {}
我有点困惑,为什么以下抛出:“错误:不能将'AClass‘类型的值转换为指定类型'T'”
class generic<T> where T:AProtocol, T:AnotherProtocol {
let prop: T = AClass()
}
但是,下面的功能很好:
class generic<T> where T: AProtoc
我想做一些可能是非正统的事情(如果我们诚实的话,在边缘是无用的),所以我们现在开始:
我希望将文字作为泛型参数传递,然后实例化它。请考虑以下示例:
const log = console.log;
class Root<T = {}> {
// public y: T = {}; // this obviously doesn't work
// again this won't work because T is used a value. Even if it worked,
// we want to pass a literal
// p
为什么Typescript人员要创建infer关键字?根据的说法,这是一个如何使用它的示例:
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
我不明白为什么需要这样做。为什么不能这样:
type ReturnType<T> = T extends (...args: any[]) => R ? R : any;
为什么这个不起作用?为什么infer关键字是必需的?
我有以下代码:
public class Generic1<T, T2> where T : Generic2<T2>
where T2 : new()
{
}
public class Generic2<T> where T : new()
{
public T GetT()
{
return new T();
}
}
这样就有可能做到:
var g = new Generic1<Generic2<int>, int>();
但是我真正想要的是像这样使用它(但是保留必须是Gen