我已经用typescript和npx create-next-app --example with-typescript with-typescript-app ()开始了一个新的Next.js项目。
无论何时,当我尝试创建这样的类时:
export class Programm {
id: string;
name: string;
thumbnailUrl: string;
}
我得到语法错误:
Property 'id' has no initializer and is not definitely assigned in the constru
我正在用TypeScript 2.6编写一些图形代码,我在继承和泛型代码方面有一个小障碍:
abstract class Series { /**/ }
class LineSeries extends Series { /**/ }
class BarSeries extends Series { /**/ }
class ScatterSeries extends Series { /**/ }
class Chart {
private seriesArray: Series[] = [];
constructor(series: Series[]) {
正如标题说的那样,我似乎不能细化对象的键,我想知道我做错了什么。
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`
我试图在另一个方法或内部方法中使用计算属性,并获得错误:Property 'comments' does not exist on type 'Vue'
import { Component, Vue } from 'vue-property-decorator'
@Component({
computed: {
comments(): Comment[] {
return this.$store.getters['comments/getComments'];
},
hasComments
我有一个抽象类,以及在数组中扩展它的类。我应该在数组中输入什么?
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
我尝试使用vue-injector,但它使用我的Vue (2.6.10)和Typescript (3.4.5)版本编译。 我几乎看不到其他选择。 在纯粹的typescript方面,我看到了像Inversify和TSyringe这样的东西,它们看起来很好,但不能开箱即用。 这是inversify: Argument of type typeof App' is not assignable to parameter of type 'VueClass<Vue>'.
Type 'typeof App' is not assignab
我是TypeScript.Currently的新手,我正在尝试学习TypeScript.Here中的界面是我的代码:
interface ClockInterface{
currentTime:Date;
setTime(d:Date):void;
}
class Clock implements ClockInterface{
currentTime:Date;[**[ts] Property 'currentTime' has no initializer and is not definitely assigned in the construct
在C#中,接口和类之间有很大的区别。实际上,一个类代表一个引用类型,因此我们实际上可以创建基于该类建模的对象,而接口则是类签名的契约,以确保某种行为的存在。特别是,我们不能创建接口实例。
接口的全部要点是公开行为。类通过给出所述行为的一个显式实现来实现它。
在这种情况下,虽然接口可能包含属性,但由于行为问题,大多数时候我们都关心接口。因此,大多数类型的接口只是行为契约。
另一方面,在TypeScript上,我似乎有些事情让我感到非常不安,事实上,我不止一次看到这个问题,这也是我提出这个问题的原因。
在一篇教程中,我看到了这一点:
export interface User {
name
我试图在TypeScript中实例化一个泛型类。
在C#,我会这样做的
class MyPacket<T> {...}
var t = typeof(...);
var genericContainer = typeof(MyPacket<>).MakeGenericType(new[]{ t });
var instance = Activator.CreateInstance(genericContainer);
我的用例是,我正在接收来自网络的消息。我还有一个EventEmitter,它期望被调用的类型是正确的。
(msg: Message): void =&g
我正尝试在TypeScript中构建一个对象工厂,其中生成的对象必须具有通用的基类型,但我还没有弄清楚如何正确地对其进行编码。 这是我现在的尝试,这是不正确的,因为TypeScript说T不能保证是Base类型。 class Base {
constructor() {}
}
class User extends Base {
constructor() {
super()
}
}
class Post extends Base {
constructor() {
super()
}
}
function bui
我想在TypeScript中创建一个类数组。这在香草JavaScript中是可能的:
class A {
constructor() {console.log('constructor');}
a() {}
}
const array = [A];
new (array[0])(); // Prints 'constructor'
我希望使用一个接口来保证数组类型的安全性。这是我在TypeScript中实现这一点的尝试:
interface I {
a();
}
class A implements I {
construc
我正在尝试弄清楚如何在JS中将TypeScript提供的类型安全与旧的普通构造函数结合使用。我有一个非常简单的例子,看起来很简单,但是我遗漏了一些东西,并且不能用TypeScript编译它:
interface IMyService {
new(): IMyService //I'm not sure if this line should be here, I just trying to make it working...
doSomething(name: string): void
}
function MyService(this: IMyService)
我正在编写一个通用的DeepReadonly类型:
type DeepReadonly<T> = {
readonly [P in keyof T]: true;
};
type A1 = DeepReadonly<number>;
type A2 = DeepReadonly<string>;
type A3 = DeepReadonly<boolean>;
type A4 = DeepReadonly<symbol>;
type A5 = DeepReadonly<never>;
type A6 = Deep
如果TypeScript是JavaScript的一个严格的超集,为什么对任意对象的点表示法是错误的?我有JS代码,为了更好的类型安全性,我想把它转换成TS,但是所有使用点表示法(例如,myObj.thing)的访问都给了我错误的Property 'thing' does not exist on type '{}'.。当我使用括号表示法(例如,myObj['thing'])时,它正常工作。