object-literal

Can you declare a object literal type that allows unknown properties in typescript?

偶尔善良 提交于 2020-06-08 16:15:13
问题 Essentially I want to ensure that an object argument contains all of the required properties, but can contain any other properties it wants. For example: function foo(bar: { baz: number }) : number { return bar.baz; } foo({ baz: 1, other: 2 }); But this results in: Object literal may only specify known properties, and 'other' does not exist in type '{ baz: number; }'. 回答1: Yes, you can. Try this: interface IBaz { baz: number; [key: string]: any; } function foo(bar: IBaz) : number { return bar

Simpler way to check literal object type in TypeScript

六眼飞鱼酱① 提交于 2020-04-11 11:41:43
问题 I want to Object.assign to an object of a known type, a set of properties from an object literal that should be of the same type. Is there a cleaner way to do this in TypeScript, without actually calling an identity function, or creating a separate variable, as suggested in this related question? type Person = { first?: string; last?: string; } // Overkill, actually generates code to call a function function check<T>(value: T): T { return value; } const dude: Person = { first: 'Mike', };

Extending object literal

两盒软妹~` 提交于 2020-01-24 10:47:08
问题 var x = { name: "japan", age: 20 } x.prototype.mad = function() { alert("USA"); }; x.mad(); The above code does not work. object literals cannot be extended? or x.mad() not the right way to call. 回答1: You can't do it this way. To be able to define object methods and properties using it's prototype you have to define your object type as a constructor function and then create an instance of it with new operator. function MyObj() {} MyObj.prototype.foo = function() { // ... } var myObj = new

How to create 2nd level property with for JS object using bracketss

廉价感情. 提交于 2020-01-24 01:39:06
问题 Here's deal. I'm starting with an object literal. var prepObj = {}; Then I will be doing a foreach where I'll be adding properties to this object. Basically, in the end it will look something like this (on a larger scale). { 34 : { prop1: myvalue, prop2: myvalue, } 87 : { prop1: myvalue, prop2: myvalue, } 102 : { prop1: myvalue, prop2: myvalue, } } Throughout the loop I'll be finding a value for a certain property of a certain index (such as prop1 of 87), and creating it for the object. So,

Define constructor prototype with object literal

浪尽此生 提交于 2020-01-16 05:07:06
问题 Which method below is best to define a constructor prototype and why? Method 1: MyConstructor.prototype.myFunction1 = function(){}; MyConstructor.prototype.myFunction2 = function(){}; Method 2: MyConstructor.prototype = { myFunction1: function(){}, myFunction2: function(){} }; I'm mostly concerned about speed. Thanks! 回答1: I would say there wouldn't be much of a difference. Using an object literal to assign to the Object.prototype is something you can't do if you're assigning the prototype

How flatten object literal properties?

喜夏-厌秋 提交于 2020-01-06 16:18:12
问题 I have an object being returned by a legacy server and I want to change the structure on the client-side via JavaScript, jQuery, or even Underscore.js. Below is what my original object looks like: [ { "Id":{ "LValue":1, "Value":1 }, "Date":{ "LValue":"2013-10-17T00:00:00", "Value":"2013-10-24T00:00:00" }, "User":{ "LValue":508, "Value":507 }, "Comments":{ "LValue":"This a test load", "Value":"This a test" }, "Name":"John Doe", "IsDeleted":false } ] On the client-side though, I would like to

ReactJS need to conditionally display different onclick vs Link based on logic

*爱你&永不变心* 提交于 2020-01-06 06:59:54
问题 Goal I have it to display different HTML with onclick vs. route Without logic in the Render() const contents = this.state.data.map(item => ( This is the logic I'm struggling with <button id={item.Member_ID} type="button" {` ${this.isExist(item.Member_ID) ? <Link to='surveysai/'> <button type="button" className='btn btn-success'>SAI</button> </Link> : ${onClick={(e) => this.downloadUser(item.Member_ID, e)}}`} className={`btn ${this.isExist(item.Member_ID) ? "btn-success" : "btn-warning"}`} >

How to create an array of object literals in a loop?

杀马特。学长 韩版系。学妹 提交于 2019-12-28 03:15:08
问题 I need to create an array of object literals like this: var myColumnDefs = [ {key:"label", sortable:true, resizeable:true}, {key:"notes", sortable:true,resizeable:true},...... In a loop like this: for (var i = 0; i < oFullResponse.results.length; i++) { console.log(oFullResponse.results[i].label); } The value of key should be results[i].label in each element of the array. 回答1: var arr = []; var len = oFullResponse.results.length; for (var i = 0; i < len; i++) { arr.push({ key: oFullResponse

Efficiently find duplicate data property in object literal?

倖福魔咒の 提交于 2019-12-24 23:57:43
问题 After creating our new system in Vue with babel, I have started testing compatibility towards older devices. Our babel is transpiling the source down to es2015 together with webpack. I have now tested with browserstack against both Ios & android. Newer OS works on both platforms. However on android phones that use the default browser, I get an error in sentry stating; Duplicate data property in object literal not allowed in strict mode It does not give me any hints on where this might be thus