我在强输入我的Twitter小部件时遇到了困难。
目前,它抛给我一堆错误,包括:
ESLint: Unsafe call of an any typed value.(@typescript-eslint/no-unsafe-call)
ESLint: Unsafe member access .catch on an any value.(@typescript-eslint/no-unsafe-member-access)
ESLint: Unsafe member access .createTweet on an any value.(@typescript-eslint/no-uns
我在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
由于可以使用VueJS v-bind:style绑定设置CSS变量,因此我正在尝试创建一个联合类型,其中传递给v-bind:style的对象保留了CssStyleDelcaration的类型,但可以轻松地接受任意属性名称:
type Dictionary<T> = { [key: string]: T };
type Nullable<T> = T | null;
type CssStyleObject =
Partial<CSSStyleDeclaration> |
Dictionary<Nullable<string>>;
export function blah<T extends { [key: string]: number[] }>(b: T, p: string) {
b[p].push(3); // valid typescript
b[p] = []; // ts(2536) Type 'string' cannot be used to index type 'T'.
}
文档中是否有涵盖此行为的部分内容,以便我能更好地理解它?
我写TypeScript已经有一段时间了,我对索引签名意味着什么感到困惑。
例如,此代码是合法的:
function fn(obj: { [x: string]: number }) {
let n: number = obj.something;
}
但是,这段代码基本上是一样的,但它不是:
function fn(obj: { [x: string]: number }) {
let p: { something: number } = obj;
}
这是个虫子吗?这意味着什么?
我有一个具有以下结构的JavaScript对象:
{
"_index": 1,
"foo": { ... },
"bar": { ... },
"baz": { ... },
}
也就是说,对象总是有一个带有数字值的属性_index,然后所有其他键(因为它被用作映射而没有定义)都有另一个特定对象的值。
我在TypeScript中尝试了以下类型
type Properties = {
_index: number
}
type Fields = {
// its not important what Me
我在读“ 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
我所知道的
当使用具有角的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
我想要创建一个对象接口,它强制使用字符串作为键,但是TypeScript编译器传递代码,即使它有一个数字类型作为键。为什么?
例如,
interface PriceI {
[key:string]:number;
}
var coursePrice: PriceI = {};
coursePrice["Orange"] = 100;
coursePrice["Apple"] = 200;
coursePrice[3]=200; // It should be compile error, but it pass
coursePrice[true]
我正在尝试替换的代码:
this.selectedArray1.indexOf(someIndexObject);
我尝试将其替换为:
var someVariable = "selectedArray1"
this[someVariable].indexOf(someIndexObject);
但是,当我执行上面的替换时,它会给我一个编译错误
TS2538: Type 'String' cannot be used as an index type
在typescript中可以做这样的事情吗?
我有一个简单的代码:
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
在尝试使用类型时,我反复尝试使用方括号获取和设置元素,从而对自己开枪。我意识到我需要使用map.get(key)和map.set(key,value),但是在使用了很长时间的Python和C#字典之后,我很难从我的厚厚的头盖骨中找到它。
let m = new Map<string, string>();
// This doesn't generate any error, though it
// doesn't do what someone might assume it
// would (doesn't add an element to
如果对象中的属性存在且大于0,则尝试将其减少1,但无论我尝试什么,TypeScript都不会编译这段代码。
interface IStatus {
[key: string]: number | undefined; // <= I don't want undefined here but it complains since the params are optional
burning?: number;
firing?: number;
}
class Thing {
public status: IStatus = {};
private proc
我在TypeScript代码中遇到了一些奇怪的情况:
var a : { [name: string]: any } = { a: 'A' }; // WORKS!
interface T { [name: string]: any };
var b : T = { a: 'A' }; // WORKS!
interface Q<a> { [name: string]: a };
var c : Q<any> = { a: 'A' }; //
我这样定义一个AbstractModel:
export interface AbstractModel {
[key: string]: any
}
然后我将类型声明为Keys
export type Keys = keyof AbstractModel;
我希望任何具有Keys类型的内容都会被统一地解释为一个字符串,例如:
const test: Keys;
test.toLowercase(); // Error: Property 'toLowerCase' does not exist on type 'string | number'. Pro