Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:26:50+00:00 2026-05-26T11:26:50+00:00

While playing around with Google Closure Compiler, I found a case where I couldn’t

  • 0

While playing around with Google Closure Compiler, I found a case where I couldn’t force the compiler to give a warning for a wrong variable type. I used the following sample:

/** @typedef ({name: string, token: string}) */
var pendingItem;

/** @type (Array.<pendingItem>) */
var pending = [];

// NOTICE: the token is intentionally misspelled as "toke" to cause a warning
var dummyItem = {
  name: 'NameHere',
  toke: 'SomeToken' 
};

// This should cause a warning, because of the 
// type mismatch (toke instead of token)
pending.push(dummyItem);

// Do something useful so that the whole code wouldn't be optimized to 0 bytes
alert(pending.length);

And it compiles perfectly, instead of giving the expected type warning. But if you use:

pending[1] = {
  name: 'Second Name',
  toke: 'Second Token'
}

You get the expected type mismatch warning.

I understand that it’s probably because push doesn’t have type-checking defined, since it’s a built-in function. It would be expected that defining pending as an array of pendingItem would force it, but it doesn’t by default.

The question is whether and how is it possible to add type checking to already defined functions such as push, so that it would give a warning in the example above. I also understand that one way to achieve a similar result would be to add /** @type {pendingItem} */ before dummyItem to force the type, but for educational purposes, I would like to know about adding type checking to functions like push (or maybe stricter definition of pending itself).

Also could somebody explain what is the logic behind renaming object properties? If you compile the above example, it wouldn’t rename dummyItem‘s property name, but it would rename token to a.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:26:50+00:00Added an answer on May 26, 2026 at 11:26 am

    To force type checking on push, it has to be redefined in code with the correct type. Note that using object literal notation, you have to define the types by using @type instead of @param, because we are assigning the function to a object’s parameter, not defining a normal function. In this case it would be the following:

    /** @type {function(pendingItem)} */
    pending.push = function(item) {
      Array.prototype.push.call(pending, item);
    };
    

    Now when trying to compile the following:

    // Still an intentionally misspelled "toke"
    var dummyItem = {
      name: 'NameHere',
      toke: 'SomeToken' 
    };
    
    // First warning
    pending.push(dummyItem);
    
    // Second warning
    pending[1] = {
      name: 'Second name',
      toke: 'Second Token'
    };
    

    Then both will generate a type mismatch warning, like expected.

    Thought that it might be useful to somebody in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've playing around with Google App Engine and Google Datastore for a while now
While playing around with as' vector.sort() I found out that it behaves normally in
I have been playing around with wf4 for a while. I found various articles,
While playing around with regexps in Scala I wrote something like this: scala> val
There is an eval() function in Python I stumbled upon while playing around. I
So, I am learning Javascript while playing white Google Calendar APIs and I just
I've been looking around Google and SO and haven't quite found an answer to
While playing around with the emulator, I noticed that when trying to view a
I've been playing around the Facebook Graph API for a while now. I'm trying
While playing around with one-to-one associations in castle activerecord I stumbled upon the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.