我有一个具有以下结构的JavaScript对象:
{
"_index": 1,
"foo": { ... },
"bar": { ... },
"baz": { ... },
}
也就是说,对象总是有一个带有数字值的属性_index,然后所有其他键(因为它被用作映射而没有定义)都有另一个特定对象的值。
我在TypeScript中尝试了以下类型
type Properties = {
_index: number
}
type Fields = {
// its not important what Me
我的应用程序有很多TypeScript错误,但运行正常(我将一个javascript应用程序移到了typescript,现在无法修复所有的类型问题……)。我已经设置了我的编辑器(webstorm)来忽略这些错误,然后应用程序就会编译。
运行应用程序(JiT)运行得很好,但是当我尝试用AoT编译应用程序(我遵循了)时,我得到了所有的TypeScript错误,编译失败。我不能粘贴所有的错误(有太多的错误),但这里有一个示例:
Error at C:/app-path/services/app.service.ts:18:23: Property 'saveScroll' does
我有一些使用外部库的代码,它从服务器检索rest数据。它看起来是这样的: let value: ITypeValue & ITypeData = service.data.select('Name', 'Id', 'ChildId').get();
// ChildId is not a part of interface, but can be retrieved
forEach.value(item => (
console.log(item.Id + ' - ' + item.Name + '
我刚开始在Angular 2中使用Observable,我的结构将不会收到结果,即使我可以验证来自REST API的响应。
我在Typescript中有以下数据类。
import { RESTResult } from '../common/RESTResult'; // Common Response Structure
import { StoresSummaryData } from './StoresSummaryData'; // Structure of records being returned as an array
expo
我在TypeScript 3.2.2和strict: true上。
我无法键入具有一组动态定义键的普通对象,但所有这些键都有一个有保证的非空值类型。
[TypeScript游乐场示例]()
// Will assume any property access has type T
interface Object<T> {
[key: string]: T;
}
// Will assume Object.values() could be undefined
interface Object<T> {
[key: string]: T | undef
我是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
为什么类型检查不适用于someCat2??
interface A{
x: number;
}
function test(something: A){}
let someCat = {cat : "miao"}
function MakeCat(){
this.cat = "miao";
}
let someCat2 = new MakeCat();
test(someCat2); // --->> typescript DOES NOT marks the error <<-------
test(some
尝试学习typescript,就像typescript的类型检查一样,只是想知道为什么在下面的情况下tsc无法检测到lastName是一个数字?谢谢,
class Student {
constructor(public firstName, public lastName ) {
}
}
interface Person {
firstName : string;
lastName : string;
}
function greeter(person : Person) {
return "hello, " + person
}
var user
我试图在TypeScript中导入我的TypeScript文件,但它似乎不起作用。具体来说,我只是试图导入它,以便能够访问日志语句的名称和版本属性。类似于:
import * as pjson from '../package.json';
// other code here
log.info(`${pjson.name}:${pjson.version}` started on port ...);
在其他使用Node/Babel的项目中,我们也有相同的语法,但我试图围绕这些部分介绍一些TypeScript。在其他地方,我们会做这样的事情:
import { name, v
我在读“ in TypeScript”。我正在看这个例子。
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
// Error: indexing with a numeric string might get you a completely separate type of Animal!
interface NotOkay {
[x: number]: Animal;
// Numeric index type 'Animal' is not
我已经编码TypeScript几年了。我已经看到了它的发展,并在每一个版本中都做了很多改进。然而,所有这些静态类型的完美都会随着类型的转换而分崩离析。
尽管在TypeScript中进行了大量的编码,但我仍然不明白如何避免在生成对象转换的函数中转换。
如下面的示例所示:我希望以一个对象作为输入,并将该对象的转换副本作为输出返回。
function stringifyProperties<T>(obj: T): { [P in keyof T]: string } {
let stringified: { [P in keyof T]: string } = {} as any