提供以下代码:
export function objToStr(object: object): string {
let str = [];
for (let p in object) {
if (object.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(object[p]))
}
}
return str.join("&")
}
我从object[p]
得到了错误:
Element implicitly has an 'any' type because type '{}' has no index signature. [7017]
我尝试过
encodeURIComponent((<any>object[p]))
encodeURIComponent(object[p]: any)
但是我仍然得到错误。我发现键入所有内容都非常混乱,那里有太多类型。
如果有人可以告诉我Typescript在那儿想要我什么,那将有很大帮助,这是最后一条错误消息,然后我将我的代码形式从JS切换到TS。
编辑
我不确定是否在做什么以及代码如何对此做出反应,因此我不得不添加"noImplicitAny": true
来测试设置。
将其设置为false我现在得到:
Argument of type 'string' is not assignable to parameter of type 'never'. [2345]
用于部分现场str.push
答案 0 :(得分:1)
出现错误是因为tsconfig.json中有 async Task TryGetLocationAsync()
{
if ((int)Build.VERSION.SdkInt < 23)
{
await GetLocationAsync();
return;
}
await GetLocationPermissionAsync();
}
readonly string[] PermissionsLocation =
{
Manifest.Permission.AccessCoarseLocation,
Manifest.Permission.AccessFineLocation
};
const int RequestLocationId = 0;
async Task GetLocationPermissionAsync()
{
//Check to see if any permission in our group is available, if one, then all are
const string permission = Manifest.Permission.AccessFineLocation;
if (CheckSelfPermission(permission) == (int)Permission.Granted)
{
await GetLocationAsync();
return;
}
//need to request permission
if (ShouldShowRequestPermissionRationale(permission))
{
Android.Content.Context c = Forms.Context;
new Android.App.AlertDialog.Builder(c)
.SetTitle("Location access is required.")
.SetMessage("Show settings to enable?")
.SetPositiveButton(
"OK", (sender, e) =>
{
RequestPermissions(PermissionsLocation, RequestLocationId);
})
.SetNegativeButton("Cancel", (sender, e) =>
{
(sender as Android.App.AlertDialog).Hide();
})
.Show();
return;
}
//Finally request permissions with the list of permissions and Id
RequestPermissions(PermissionsLocation, RequestLocationId);
}
。错误提示,将索引签名添加到compilerOptions.noImplicitAny = true
变量中:
object
即let object: { [index: string]: any } = {};
let str: string[] = [];
将被明确指定。