我一直在研究并尝试将现有项目(从Node.js)转换为TypeScript。
对于上下文,我使用的是http-status包()
我试图将变量传递到它们的默认导出中,但是它得到了一个错误:
import status = require("http-status");
status.OK; // this works
status["OK"] // this also works
let str = "OK";
status[str]; // error
错误:
元素隐式具有'any‘类型,因为类型'string’的表达式不能
我想要Object.assign到一个已知类型的对象,一组应该属于同一类型的对象文本中的属性。在TypeScript中,是否有更清晰的方法可以做到这一点,而不像本中所建议的那样,实际上调用标识函数,或者创建单独的变量?
type Person = {
first?: string;
last?: string;
}
// Overkill, actually generates code to call a function
function check<T>(value: T): T { return value; }
const dude: Person = {
有人能向我澄清,TypeScript中的两个符号之间有什么区别吗?
class Test {
readonly ANSWER = 42;
}
和
class Test {
ANSWER = 42 as const;
}
第一个是,第二个是。这两个概念在多个地方都有详细的记录和解释,但它们似乎都对我做了同样的事情,至少在这个简单的例子中,即使属性不可变。甚至它们的组合似乎也是有效的:
class Test {
readonly ANSWER = 42 as const;
}
那么,为什么它们都被允许使用语法,并且使用一种比另一种更好还是更不利呢?
为了进一步澄清:我完全了解常规之间的
我看到你像<div style={{WebkitAppearance: SOMETHING}}></div>一样做这件事,但有什么了不起的呢?
type PropType = {
appearance: string;
}
function MyComponent({
appearance
}: PropType) {
const style = {
WebkitAppearance: appearance
}
}
我得到了这个错误:
Type 'string' is not assignable to type 'We
我正在用Cypress和TypeScript开发一个应用程序。
我有以下代码,它根据Magic是否在E2E测试中运行,稍微改变了它的行为:
const magic = new Magic(window.ENV.MAGIC_PUBLISHABLE_KEY, {
testMode: Boolean(window.Cypress),
});
TypeScript抱怨window.Cypress并说:
Property 'Cypress' does not exist on type 'Window & typeof globalThis'.
我怎样才能告诉
我最近在TypeScript上偶然发现了这种奇怪的行为。在编译期间,只有当期望变量的类型是接口时,如果接口没有强制字段,它才会抱怨过多的属性。链接到TypeScript游乐场#1:
interface IAnimal {
name?: string;
}
class Animal implements IAnimal {
}
var x : IAnimal = { bar: true }; // Object literal may only specify known properties, and 'bar' does not exist in type
我有一个JSON对象,我想获取“方法”( JSON对象中的属性之一),但“方法”有时存在,有时不存在。如何检查是否存在,然后只运行我的函数,如果不存在,它不会跳过这个错误“属性‘方法’不存在的类型”在typescript。
无错误
data = {
...
methods: [{ id:1, name:"abc"},{ id:2, name:"def"}]
...
};
if (this.data.methods) {
// this for loop no jump any error
for (let index = 0; index <
我是TypeScript的初学者,当我学习TypeScript接口时,我发现了一些我不理解的特性。
我已经尝试了操场上遵循的代码:
interface LabelledValue {
size?: number;
}
function printLabel(labelledObj: LabelledValue) {
}
let myObj = {label: "Size 10 Object"};
printLabel(myObj); // error: Type '{ label: string; }' has no properties in common
我面对的是-issue中的“类型转换接口”TypeScript。我不明白为什么我的界面没有干扰!请参阅我的例子如下:
interface IProduct {
age : number;
price : number;
}
class Product{
public age: number = 22;
}
let products : Array<Product> = [new Product(), new Product()];
let products_stringify = JSON.stringify(products);
let produc
我试图使用一个可空的日期时间,并将它作为actionfilter的一个参数,但是它会给出以下错误:
“‘Propertyname”不是有效的命名属性参数,因为它不是有效的属性参数类型
我以为一个快速的google会解决这个问题,但据我所知,我找不到很多关于它的信息。
这是我的过滤器的代码。
public class AddToSitemap : ActionFilterAttribute
{
public string Changefreq { get; set; }
public DateTime? Lastmod { get; set; }
public do