我需要做一些元编程,并且必须从函数类包装器继承。它工作得很好,除了它不会意识到静态方法也是继承的。它们就在那里,但是TypeScript看不到它。我怎样才能让它工作? TypeScript Playground class A {
method() {}
static staticMethod() {}
}
export interface AConstructor {
new(): A
}
export function classA(): AConstructor {
return A
}
class B extends classA() {}
new B().
我是typescript的新手,并试图将我已经输入的javascript代码转换为typescript,所以我最终使用了下面的代码。我对typescript中的类的理解如下,这可能是错误的: 1.在定义类之后,你必须声明参数以便稍后在this中使用,减速是以variableName:variableType的形式进行的。2.在构造函数中,变量可以赋值为this.variableName = x 3.在class下的methods中,变量可以与this.一起使用
在下面的代码中,我得到了如附件图片所示的错误[ts] Property 'escapeRegExp' does not
似乎在typescript类的实例方法中,typescript编译器认为'this‘是与声明类相同的类型。
例如:
class Person {
private name: string;
constructor(name) {
this.name = name;
}
public showName() {
//Typescript believes 'this' is a 'Person', but it could be anything!
var me: Pers
在TypeScript中,我将非实例变量从类中分离出来,放入与类同名的命名空间中。例如:
class Person
{
age: number;
constructor(age: number)
{
this.age = age;
}
}
namespace Person
{
export let numberOfFingers: number = 10;
}
export default Person;
与此相反的是:
class Person
{
static numberOfFingers: number = 10;
我对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(
我在一个带有静态函数的自定义Magento模型中有一个自定义模型:
class ABC_Module_Model_ClassName
{
static public function send ( $something)
{
// do something static
}
}
现在我像这样调用函数:
ABC_Module_Model_ClassName::send($something); // works and is nothing wrong with it
出于一致性的考虑,我想知道Mage是否有调用静态方法的内部方法,例如:
Mage::g
当我在搜索如何迁移到typescript我的nodejs代码时,我发现了类似这样的东西:
class Server {
public app: any;
constructor() {
this.app = express();
}
public static bootstrap(): Server {
return new Server();
}
}
它是这样使用的:
const app = Server.bootstrap().app;
我的问题是,与使用普通构造函数相比,使用静态方法的优势是什么?它是最佳实践还是取决于用例。它是一种设计模式吗?
在PHP中,当在实例中使用时,和有什么不同?
示例:
class A {
function dump() {
echo get_called_class();
echo get_class($this);
}
}
class B extends A {}
$A = new A();
$B = new B();
$A->dump(); // output is 'AA'
$B->dump(); // output is 'BB'
这种情况有什么不同吗?
我应该在什么时候使用get_called_cl
我不确定在TypeScript中处理“此”范围的最佳方法。
下面是我要转换为TypeScript的代码中一个常见模式的示例:
class DemonstrateScopingProblems {
private status = "blah";
public run() {
alert(this.status);
}
}
var thisTest = new DemonstrateScopingProblems();
// works as expected, displays "blah":
thisTest.run(
先编码。
class A
{
private A(){}
public static A.Builder Builder()
{
/**
* ERROR:
* No enclosing instance of type A is accessible.
* Must qualify the allocation with an enclosing instance of type A
* (e.g. x.new A() where x is an instance of A).
我正在尝试(以一种简化的方式)将以下示例移植到typescript中,因为我想在typescript中演示抽象工厂模式。
到目前为止,我创建的代码如下(仅限结构)
export default class Customer{
public id: string;
public firstName: string;
public lastName: string;
}
import Customer from "./Customer";
export default interface ICustomerDAO{
insertCustome
我正在寻找一种在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)
有什么解决办法吗?
考虑以下代码:
abstract class AbstractFoo {
}
class Foo extends AbstractFoo {
}
class Bar {
getFooConstructor(): typeof AbstractFoo {
return Foo;
}
}
let bar = new Bar();
let FooConstructor = bar.getFooConstructor();
let foo = new FooConstructor();
它无法编译,出现以下错误:
错误TS2511:无法创建抽象类的实例。
我正在尝试在类中创建一个方法,该方法将实例化当前在其中的类。但是,我也需要这个方法在所有扩展类中正确工作。正如我从中学到的,在这个任务中使用self关键字是不好的。所以很明显的选择是使用static关键字。
但是,我遇到了同样有效的不同方法。
示例:
class SimpleClass
{
private $arg;
public function __construct( $arg ){
$this->arg = $arg;
}
public function getArg(){return $this->arg;}
p
我想了解为什么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
我目前正在SvelteKit应用程序中的SvelteKit文件中使用以下内容:
export async function handle({ event, resolve }) {
console.log(event.locals) //<-- Works fine
}
我试图弄清楚如何在event和resolve参数上使用类型。据我所知,event的工作方式如下:
import type { RequestEvent } from '@sveltejs/kit'
export async function handle(event: RequestEvent, r
在TypeScript中输入功能性反应本机组件的最佳实践中,我在遵循当前的观点时遇到了困难。我发现许多帖子和线程似乎从一种观点开始,然后随着时间的推移而转移到另一种观点,而没有达成明确的共识。
对此有目前的看法吗?例如,是否有以下任何一种首选,或者完全是其他的?
// Version ONE (makes most "sense" to me)
const MyComponent = (props: MyComponentProps): JSX.Element => {
return (
//... some JSX
)
}
// Ver