我所知道的
当使用具有角的ui状态的TypeScript时,我可以提供“类型断言”和UI-路由器绝对类型的库。
使用此方法,我可以注入$state并具有类似于以下代码的代码
function myCtrl($state: ng.ui.IStateService){
// Some code
}
这为$state的方法提供了正确的自动完成/错误报告。
到目前为止,一切都很好。
问题所在
当我尝试访问params的属性时,如下所示
function myCtrl($state: ng.ui.IStateService){
// Trying to access a property
我有一个简单的代码:
const allTypes = { jpg: true, gif: true, png: true, mp4: true };
const mediaType = url.substring(url.lastIndexOf('.') + 1).toLowerCase();
return Boolean(allTypes[mediaType]);
TypeScript正在抱怨:
Element implicitly has an 'any' type because expression of type 'string' c
如何在typescript中传递所需参数的条件类型参数? type SomeType = string | string[]
const a: SomeType = "some string"
function person (param: string) {
}
person(a) // gives error in lint => Type 'string[]' cannot be used as an index type
我试图在Typescript中更深入地理解高级类型,其中一种类型是NonFunctionPropertyNames,它只提取给定对象的一个属性。 type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]; 我可以理解花括号'{ [K in keyof T]: T[K] extends Function ? never : K }'中的第一部分,我们声明了一个对象,并排除了扩展函数的属性。让我困惑的是花括号[keyof T]后面的
在编写这段TypeScript代码时,我惊讶地发现,当我将字符串转换为数字时,TypeScript编译器没有警告我。
var a = new string[];
a.push('a')
a.push('b')
a.push('c')
var i:string;
for (i in a) {
var c : number = a[i]; <---- Here a is string[] but I assign it to c which is number
alert(c.toString())
}
我有两个交易,一个接一个地开始:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
select * from MyTable WITH (XLOCK) WHERE Id = 1
WAITFOR DELAY '00:00:10';
COMMIT TRANSACTION
第二个是几乎相同的(只是没有延迟和另一个id)。
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
select * from MyTable WITH (X
我写TypeScript已经有一段时间了,我对索引签名意味着什么感到困惑。
例如,此代码是合法的:
function fn(obj: { [x: string]: number }) {
let n: number = obj.something;
}
但是,这段代码基本上是一样的,但它不是:
function fn(obj: { [x: string]: number }) {
let p: { something: number } = obj;
}
这是个虫子吗?这意味着什么?
如果我想让string:object字典在TypeScript中进行类型检查,有两种方法可以做到这一点:
const m = new Map<string, MyObject>();
m.set("a", new MyObject("a"));
m.set("b", new MyObject("b"));
和
const m : {[index: string]: MyObject} = {};
m["a"] = new MyObject("a");
m["b"] = n
我在读“ 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类:
export class Spot {
public flight:number;
public dateAndTimes:Map<string,string>
public unitCost:number;
constructor(){
this.flight=0;
this.dateAndTimes= new Map<string,string>();
this.unitCost=0;
}
}
如何将Spot对象转换为JSON?
我尝试使
我正在使用VS Code,它在使用Typescript时非常有用,因为它告诉您当前的问题不需要传输。我使用的是Typescript和nodeJS,它工作得很好。
我唯一的问题就是所谓的“空/未定义的检查函数”。
参见此示例:
class User {
public someProperty: string | undefined;
// ...
public get canDoSomething() {
return this.someProperty != undefined;
}
}
let myUser: User = ...
这样做很好:
if (
我试图在TypeScript中创建一个条件类型,它区分“记录”类型(其中键是无界/动态的)和普通对象类型(其中键是预定义的)。
type R = { [x: string]: any }
type O = { a: string; b: number }
我可以获得类型的键,并检查一些有希望肯定不存在的属性:
type R = { [x: string]: any }
type O = { a: string; b: number }
type IsRecord<T> = '__NEVER__' extends keyof T ? true : false
ty