163
rated 0 times
[
166]
[
3]
/ answers: 1 / hits: 31252
/ 8 Years ago, wed, may 18, 2016, 12:00:00
I'm using a JS library, specifically select2
that acts a tad differently than I'd like if the objects I'm passing it aren't plain objects. This is all checked by using jQuery's isPlainObject
function.
Does TypeScript have a cast I'm unaware of that would achieve this without resorting to writing my own?
class Opt {
constructor(public id, public text) {
}
toPlainObj(): Object {
return {
id: this.id,
text: this.text
}
}
}
let opts = [
new Opt(0, 'foo'),
new Opt(1, 'bar')
];
console.clear()
console.log('both should be false')
$.map(opts, opt => {
console.log($.isPlainObject(opt))
})
console.log('both should be true')
$.map(opts, opt => {
console.log($.isPlainObject(opt.toPlainObj()))
})
More From » typescript