我目前正在将一个Next.js项目从JavaScript迁移到TypeScript,并且我遇到了一个错误:Property 'className' does not exist on type '{ props: ReactNode; }'。在Javascript中,我可以从道具中提取className,但是typescript找不到类型。代码如下: import { useRouter } from 'next/router'
import Link from 'next/link'
import { ReactNode }
我已经用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
在C#中,接口和类之间有很大的区别。实际上,一个类代表一个引用类型,因此我们实际上可以创建基于该类建模的对象,而接口则是类签名的契约,以确保某种行为的存在。特别是,我们不能创建接口实例。
接口的全部要点是公开行为。类通过给出所述行为的一个显式实现来实现它。
在这种情况下,虽然接口可能包含属性,但由于行为问题,大多数时候我们都关心接口。因此,大多数类型的接口只是行为契约。
另一方面,在TypeScript上,我似乎有些事情让我感到非常不安,事实上,我不止一次看到这个问题,这也是我提出这个问题的原因。
在一篇教程中,我看到了这一点:
export interface User {
name
我正在转换一个代码库从Javascript到Typescript,并且在这个函数上有问题。我在行const valueAtKey = item[key]上收到一个输入错误,显示为Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on ty
我正在处理angular2 typescript项目,我只想在调用angular2()函数后将对象的键重置为'‘,示例如下
export class Events{
event:any={ name : '', date : '',.........};
eventOriginal=this.event;
save(){
//save(this.event)
//after save, reset value to initial state
this.event=this.eventO
在TypeScript中,我们可以使用字符串文本类型来执行以下操作:
type HelloString = "Hello";
这使我可以将类似字符串枚举的内容定义为:
namespace Literals {
export type One = "one";
export type Two = "two";
}
然后我可以定义一个联盟:
type Literal = Literals.One | Literals.Two;
是否有方法提取Literals.One作为Literals.One类型的唯一值?
原因是,当我定义这样的函数
我有一个具有以下结构的JavaScript对象:
{
"_index": 1,
"foo": { ... },
"bar": { ... },
"baz": { ... },
}
也就是说,对象总是有一个带有数字值的属性_index,然后所有其他键(因为它被用作映射而没有定义)都有另一个特定对象的值。
我在TypeScript中尝试了以下类型
type Properties = {
_index: number
}
type Fields = {
// its not important what Me