目标是在我的TypeScript代码中进行代码分割。
我使用(实验性的)装饰器来支持从模型到持久存储的类似ORM的数据映射。其中一种类型需要用表的名称参数化装饰器,该表将存储该类型的实体。为了进行代码拆分,我将域模型提取为一个单独的文件(entity-model.ts):
/* I am using dynamodb-data-mapper */
import {
table,
hashKey,
attribute
} from '@aws/dynamodb-data-mapper-annotations'
export class Entity {
/*
我有以下TypeScript代码: class ClassA {
field1: any;
}
const SomeClassAsA = diContainer.get('SomeClass') as ClassA;
class ClassB extends ClassA {} // works
class ClassB extends SomeClassAsA {} // error: Type ClassA is not a constructor function type 为什么可以用ClassA扩展ClassB,而不能用其他被转换为ClassA的对象来扩展呢
如果我用以下方式在TypeScript中定义一个类
class A {}
然后,A既是一个值,也是一个类型,所以我可以用这两种方式使用它:
type B = A
const C = A
如果A是一个高级工厂函数的结果,那么:
const A = fancyClassFactory(...someArgs)
那A只是一个值,不是一个类型..。
是否有任何方法来使用这样的类工厂函数,并得到类的类型?
在此之前,非常感谢您。
我有一个抽象类,以及在数组中扩展它的类。我应该在数组中输入什么?
abstract class AbstractClassToExtend {
constructor() { console.log("hello") }
}
class One extends AbstractClassToExtend {}
class Two extends AbstractClassToExtend {}
const array = [One, Two] // what do i type this array?
我尝试过const array: typeof Abstract
我仍然很难使用TypeScript的类型检查系统。假设一个包含元素集合的组合,这些元素都是从一个公共基类派生的。如何实现递归上升层次结构并返回给定类型的第一个锚点的函数?
abstract class Employee
{
public Superior: Employee;
/** THIS IS NOT WORKING */
public getSuperiorOfType<T extends Employee>( type: typeof T ): T
{
if (this instanceof T) return this;
使用在TypeScript 1.7中,正如我发现的,我们可以在返回类型为this的类中定义一个方法,并且自动地,任何扩展该类并继承这些方法的类都会将它们的返回类型设置为各自的this类型。就像这样:
class Model {
save():this { // return type: Model
// save the current instance and return it
}
}
class SomeModel extends Model {
// inherits the save() method - return type: SomeModel
}
我很难让TypeScript编译器不让我感到悲伤。我有一个控制器,它有一个类数组,不是类的实例,而是类本身。所有这些都将从相同的基础上延伸,UnitView。由于某些原因,如果我的TitleView也不接受泛型,我会得到一个错误,但是如果它也接受了,我就不会得到错误。
我不明白为什么TitleView需要接受泛型,因为它显式地将模型类型传递给UnitView。有人能解释或看到我做错了什么吗?
代码:
class Model { }
class View<TModel extends Model> {
private model: TModel;
}
class UnitV
谁能告诉我为什么TypeScript在MyComponent实现Rendered时会说'typeof MyComponent' is not assignable to type 'Renderer'. Property 'input' is missing in type 'typeof MyComponent'?
import { Component, Input } from '@angular/core';
interface Renderer {
input: string;
}
@Compo
我正在寻找一种在abstract static中实现TypeScript方法的方法。
这里有一个例子:
abstract class A {
abstract static b (): any // error
}
class B extends A {
static b () {}
}
这在public、private和protected方法中是允许的,但对于static方法则不允许。TypeScript返回以下错误:
‘静态’修饰符不能与‘抽象’modifier.ts一起使用(1243)
有什么解决办法吗?
据我所知,TypeScript "typeof“操作符执行类型查询,并允许您确保两个对象是相同的类型,即使该类型未命名。但是,我在生产环境中发现了一些代码,它们执行以下操作:
function println (x) {
document.write(x + '<br\>');
}
class SettingsBase {
public static setting1: string;
public static setting2: string;
}
class SettingsUser {
private _settings: typ
我正在尝试创建一个函数,它接受一个类作为参数(不是类实例,而是类本身),并返回另一个类,它扩展了参数中给出的类。
我尝试按如下方式定义typescript函数,但得到以下错误:Type 'typeof (Anonymous class)' is not assignable to type 'T'
function extendClass<T>(base: T): T {
return class extends base{
};
}
上面这个函数的用法示例:
class A {
}
const B = extendClass(A
正如标题说的那样,我似乎不能细化对象的键,我想知道我做错了什么。
const x: { key?: number } = {};
if (typeof x.key === 'number') {
console.log(x.key); // typescript sees `x.key` as `number | undefined` still
}
let y: number | undefined;
if (typeof y === 'number') {
console.log(y); // typescript sees `y`
在TypeScript中,有没有办法键入以下fn函数,以便TypeScript仍然知道result的类型是'hello' const hello = 'hello' // hello is of type 'hello'
const fn = (x: unknown) => x
const result = fn(hello) // result is of type unknown 对于泛型,可以传递类型: const hello = 'hello' // hello is of type 'hello
我想了解为什么TypeScript不能推断以下函数的返回类型(虽然它能够在if-else语句中进行区分):
function calc (arg: number|string) {
if (typeof arg === 'number') {
// here typescript knows arg is number type
return arg
} else if (typeof arg === 'string') {
// here typescript knows arg is string type
retu
为什么S在下面的代码中是string?
type T<X> = {[K in keyof X]: never};
type S = T<string>;
这是TypeScript游乐场。
即使我用其他类型替换never,它也会变成相同的类型(never)。
添加上下文,下面的类型将如我所期望的那样工作。
type T<X> = {[K in (keyof X) & string]: never};
type S = T<string>;
我有下面的typescript类,以从localStorage获得一个模型
export class LocalStorageHelper {
public static GetItemValue<T>(key: string): T {
let value: string = localStorage.getItem(key);
// if(typeof T == 'string') return value;
// return (Convert JSON.parse(value) To T)
}
我有一个函数URLBuilder,它作为一个模块工作(它返回一个包含各种函数的对象,这些函数与上下文对象绑定得很好,所以this绑定按预期工作,我们可以在这些函数中正确地访问this ) 我的问题是为什么typescript不理解URLBuilder的返回对象中函数的this绑定?尽管我尝试手动添加一个返回类型,但Typescript抱怨when using the URLBuilder在没有this上下文的情况下无法接受函数。 我们如何让typescript在返回的对象中理解这个绑定函数的this? 我要解决的Typescript错误是: The 'this' contex
这是简化的代码
export interface MyInterface{
// Have no idea why I need an export here.
// If I remove it, it gives
// semantic error TS2451 Cannot redeclare block-scoped variable 'createClass'
}
const createClass = function () {
return class implements MyInterface{
}
};
const
我有一个实现IDataReader的更干净的类,它所做的是过滤SQL Server的DateTime范围之外的任何DateTime值,并返回DBNull (这个类的输出被馈送到中,我的数据源可以返回早于1/1/1753的日期)
我的问题是其中一个函数的接口返回一个新的IDataReader,我希望的是任何派生类都不需要重写函数来返回新对象本身。下面是一个例子
public class SqlServerDataCleaner : IDataReader
{
public SqlServerDataCleaner(IDataReader dataSource)
{
我希望这个代码示例对我想要做的事情足够清楚。
请注意,这是一个简化的测试用例,以演示我遇到的核心问题。我不想知道如何使这个例子更简单。我想知道如何阻止TypeScript抱怨。我也不想使用any类型。
// start with a class
class HelloWorld {
hello = 'hello'
world(): string {
return this.hello + ' world'
}
}
// Create an interface for an object
interface Greeti
我正在尝试创建一个函数,它将接受另一个函数,当调用该函数时,它将解析为特定类型的值,或者如果传递了该值,则只返回该值。 但是,我不能将其范围缩小到最后的值。 type Test<T> = (() => T) | T
class List{
constructor(protected fnOrNumber: Test<number>) {
//fnOrNumber will sometimes be number or function that returns number
}
get list(): number{
我有这样一个实用程序函数,主要是为了避开typedef null是'object'的烦恼。
export function isObject(a: any): a is object {
return a && (typeof a === 'function' || typeof a === 'object');
}
与声明此函数boolean的返回类型不同,您可以使用a is object,它本质上也是一个boolean,但它有助于类型推断,以便TypeScript知道,如果该函数为真,则a可以被视为object,而无需进一
我想引用类构造函数本身的类型,而不是它的实例。
class A {
static a = 1;
b = 2;
}
let a: A; // a is an instance of A
a.b === 2 // this works as expected
let aClass: A['constructor'];
aClass.a // not found in Typescript
我对TypeScript (1.8)非常陌生,在继承和静态属性方面有一个小问题。
请在下面找到我正在运行的测试代码:
class A {
public static Items = {
FOO: 'A'
};
public show() {
alert(this.constructor.Items.FOO);
}
}
class B extends A {
public static Items = {
FOO: 'B'
};
}
var a = new A(