相关疑难解决方法(0)

在TypeScript中运行时获取对象的类名

是否可以使用typescript在运行时获取对象的类/类型名称?

class MyClass{}

var instance = new MyClass();
console.log(instance.????); // Should output "MyClass"
Run Code Online (Sandbox Code Playgroud)

typescript

233
推荐指数
8
解决办法
19万
查看次数

TypeScript:如何从类型中提取泛型参数?

假设我有类似的类型React.ComponentClass<Props>,我想引用该Props部分,但它在我当前的上下文中未命名.

为了显示:

typeof x

在这种情况下,我可以使用,Something但这不会让我直接访问该Something类型.

我想要的是这样的:

GetPropsType<typeof x>

所以我可以提取React.ComponentClass<Props>类型Props

任何相当的东西都会很棒.

typescript

10
推荐指数
1
解决办法
2904
查看次数

TypeScript 中的关联类型

Swift 中关联类型这样的概念。

protocol Container {
    associatedtype ItemType // Here it is.
    mutating func append(item: ItemType)
    var count: Int { get }
    subscript(i: Int) -> ItemType { get }
}

struct IntStack: Container {
    typealias ItemType = Int // Here again.
    mutating func append(item: Int) {
        self.push(item)
    }
    var count: Int {
        return items.count
    }
    subscript(i: Int) -> Int {
        return items[i]
    }

    var items = [Int]()
    mutating func push(item: Int) {
        items.append(item)
    }
    mutating func pop() …
Run Code Online (Sandbox Code Playgroud)

generics typescript

9
推荐指数
2
解决办法
1711
查看次数

TypeScript - 获取泛型类参数的类型

我需要实现一个GetClassParameter<T>像这样工作的类型:

class Foo<T> {

}

type foo = Foo<string>

type x = GetClassParameter<foo>; // should be string
Run Code Online (Sandbox Code Playgroud)

很抱歉,如果它是重复的,我找不到它。我只找到了一个硬编码的解决方案(来源):

type GetFooParameter<T extends Foo<any>> = T extends Foo<infer R> ? R : unknown;
Run Code Online (Sandbox Code Playgroud)

我试图做这样的事情:

class Foo<T> {
    public value: T;
    public value2: string;
    constructor (value: T) {
        this.value = value;
    }
}

type BaseT<T> = {
  value: T  // if removed, it wouldn't work
}
type GetClassParameter<T extends BaseT<any>> = T extends BaseT<infer R> ? R : unknown; …
Run Code Online (Sandbox Code Playgroud)

javascript typescript typescript-generics typescript-typings

6
推荐指数
1
解决办法
412
查看次数

如何在Typescript的泛型方法中获取T的类型?

我在一个类中有一个通用方法。

export class BaseService {
    public getAll<T>(): Observable<T> {
        // get type of T

        // const type = typeof(T); // Tried this but getting compilation exceptions 

        return this.http.get<T>(this.actionUrl + 'getAll');
    }
}
Run Code Online (Sandbox Code Playgroud)

我将从其他一些打字稿类中调用如下所示的方法。

this.service.getAll<SubscriberData>().subscribe(response => {
      // Response
    }, error => {
      console.log(error);
    }, () => {
      // do something commonly
    });
Run Code Online (Sandbox Code Playgroud)

当我尝试此操作时出现以下异常

const type = typeof(T); 
Run Code Online (Sandbox Code Playgroud)

“ T”仅指类型,在此被用作值。

编辑:

我试图获取正在调用通用方法的类的类型。例如:getAll<SubscriberData>我想获取该SubscriberData方法内的类型。

我怎样才能做到这一点?

generics typescript angular

5
推荐指数
1
解决办法
2778
查看次数

Angular 单元测试:使用泛型创建组件

我正在尝试在 周围创建一个通用包装器TestBed.createComponent,它接受一个类型参数并为该类型创建组件。但是,该TestBed.createComponent函数采用类型为 的参数Type<T>。我无法Type<T>从传入的泛型 T 参数中创建一个。

export function createTestHarness<T>(): TestHarness<T> {
  let component: T;
  let fixture: ComponentFixture<T>;

  fixture = TestBed.createComponent<T>(**PROBLEM AREA**);
  component = fixture.componentInstance;
  fixture.detectChanges();

  return new TestHarness<T>(component, fixture);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法Type<T>从传入的类型派生 a ?

unit-testing typescript typescript-generics angular

5
推荐指数
2
解决办法
1651
查看次数

Typescript:获取泛型类型的类型

我是 Typescript 的新手(来自 C#),并且正在努力解决“特殊”通用实现。

这是我的代码:

function createValue<TValue extends string | boolean>(): TValue[]
{
    let result = new Array<TValue>();

    if (typeof TValue === "string") // error 'TValue' only refers to a type, but is being used as a value here
    {
        result[0] = "abc" as TValue;
    }
    else
    {
        result[0] = false as TValue;
    }

    return result;
}

console.log(createValue<boolean>());
Run Code Online (Sandbox Code Playgroud)

我是如何得到类型的?

我已经尝试过相当丑陋的解决方案来创造价值,但这也没有达到预期的效果。

let value : TValue = new Array<TValue>(1)[0];
let type = typeof value; // undefined
Run Code Online (Sandbox Code Playgroud)

对于对象类型存在多种解决方案,但我没有得到基元的解决方案。

你能给我任何帮助吗?

谢谢

generics typescript

5
推荐指数
1
解决办法
1万
查看次数

如何在 Angular 服务中获取泛型类型 T 的名称

需要根据传递给此服务的泛型类型 T 在 Angular 5 服务中创建一些工厂方法。如何获取泛型类型“T”的名称?

@Injectable()
export class SomeService<T> {

    someModel: T;

    constructor(protected userService: UserService) {

        let user = this.userService.getLocalUser();
        let type: new () => T;

        console.log(typeof(type)) // returns "undefined"
        console.log(type instanceof MyModel) // returns "false"

        let model = new T(); // doesn't compile, T refers to a type, but used as a value

        // I also tried to initialize type, but compiler says that types are different and can't be assigned

        let type: new () => T = …
Run Code Online (Sandbox Code Playgroud)

typescript angular

1
推荐指数
1
解决办法
5573
查看次数