How to access object attributes based on a variable in Typescript

Typescript is obscurely particular with accessing attribute keys on objects that lack a generic signature. Adding generic signatures reduces type-safety though.

Here’s a Typescript-friendly way to verify an attribute exists in an object, and then access that attribute.

// implicitly typed object
const myObj = {
  Hello: "world"

const myObjKey = "Hello";

// The commonly recommended way to check if an attribute exists,  
// which throws a no signature lint error Element implicitly 
// has any type (TS7053) in Typescript
// if (myObj[myObjKey]) {
//     ...
// }

// The 'in' way, which Typescript likes
if (myObjKey in myObj) {
  // Now that we've confirmed the attribute exists, it's
  // type-safe to recast myObjKey and access it as an attribute
  console.log(myObj[myObjKey as keyof typeof myObj]);

This post originally posted at

Drupal 7: Converting a text field to a long text (aka textarea) field

Realize later on that text field you created needs to actually be a long text field?  You’re not alone.

After doing some research, I found zero complete solutions.  The ones I found leave problemattic remnants in the database, which can cause hard to diagnose issues such as restricted field length.  See references.

Luckily for you, the drush script below will do a more complete conversion.

Thanks go out to the references though, for getting me started.


 * Change a field from textfield to textarea
 * Unlike other guides online, this one actually corrects the blob values
 * Run this using drush: `drush scr <filepath>`

$field_name = "field_test_text_field_2";

$rows = db_query("SELECT id,data FROM {field_config} WHERE field_name = '{$field_name}' ")->fetchAll(PDO::FETCH_OBJ);

foreach ($rows as $row ){
 drupal_write_record("field_config", $row, "id");

// Update field_config_instance
$rows = db_query("SELECT id,data FROM {field_config_instance} WHERE field_name = '{$field_name}'")->fetchAll(PDO::FETCH_OBJ);

foreach ($rows as $row ){
 drupal_write_record("field_config_instance", $row, "id");

// Update the field table
db_change_field("field_data_{$field_name}", "{$field_name}_value", "{$field_name}_value", array(
 "type" => "varchar",
 "length" => "20000",
db_change_field("field_revision_{$field_name}", "{$field_name}_value", "{$field_name}_value", array(
 "type" => "varchar",
 "length" => "20000",

// Clear caches.