String / string
/*
function checkObjectKeyExist(object: Object, key: String): Boolean {
return key in object;
}
// The left - hand side of an 'in' expression must be a private identifier or of type 'any', 'string', 'number', or 'symbol'.ts(2360)
*/
// function checkObjectKeyExist(object: Object, key: String): Boolean {
// return "key" in object;
// }
function checkObjectKeyExist(object: Object, key: string): Boolean {
return key in object;
}
console.log(checkObjectKeyExist({
name: "xgqfrms",
age: 23,
}, "name"));
https://stackoverflow.com/questions/14727044/typescript-difference-between-string-and-string
Object vs object
class SVGStorageUtils {
// Object
store: Object;
// object
constructor(store: object) {
this.store = store;
}
// string primitive
setData(key: string = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
// String Object
getData(key: String = ``) {
const obj = JSON.parse(sessionStorage.getItem(key));
}
clear(key: any) {
delete this.store[key];
}
clearAll() {
this.store = {};
}
init() {
this.store = {};
}
}
TypeScript: String vs string
Argument of type 'String' is not assignable to parameter of type 'string'.
'string' is a primitive, but 'String' is a wrapper object.
Prefer using 'string' when possible.
demo
String Object
// error
class SVGStorageUtils {
store: object;
constructor(store: object) {
this.store = store;
}
setData(key: String = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
getData(key: String = ``) {
const obj = JSON.parse(sessionStorage.getItem(key));
}
}
string primitive
// ok
class SVGStorageUtils {
store: object;
constructor(store: object) {
this.store = store;
}
setData(key: string = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
getData(key: string = ``) {
const obj = JSON.parse(sessionStorage.getItem(key));
}
}
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ????️,侵权必究⚠️!
|
请发表评论