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

  • SEARCH
  • Home
  • 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 8706367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:35:48+00:00 2026-06-13T03:35:48+00:00

When writing this TypeScript code I was surprised to see that TypeScript compiler does

  • 0

When writing this TypeScript code I was surprised to see that TypeScript compiler does not warn me when I convert from string to number.

var a = new string[];
a.push('a')
a.push('b')
a.push('c')

var i:string;
for (i in a) {
    var c : number = a[i];  <---- Here a is string[] but I assign it to c which is number
    alert(c.toString())
}
  • 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-06-13T03:35:50+00:00Added an answer on June 13, 2026 at 3:35 am

    Short answer: a[i] is of type any, not string.

    Long answer: a is of type string[]. In TypeScript, objects of type T[] that are indexed by a number result in a value of type T, but indexing them by a string results in an any. Values of type any can be freely converted to any other type.

    Why?

    When you index an array by a string, the compiler simply has no ability to predict what’s going to happen, and you’re stuck with any. Consider:

    // Elsewhere...
    Array.prototype.danger = 35;
    
    // Meanwhile, back at the ranch:
    var a = [ 'z' ];
    
    var i: string;
    for (i in a) {
        var x = a[i]; // i = 'danger', a[i] = 35...
    }
    

    If you’re really confident that no one is going to be mucking with your object, or you’re taking indexes from a known-safe set of string values, you can write an indexing type. In the case where you’re indexing by a string, you’re probably better off using an object rather than an array:

    var a: {[s: string]: string; } = {};
    a['hello'] = 'world';
    var i:string;
    for (i in a) {
        var c : number = a[i]; // Issues an error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've had trouble writing this code. I'm supposed to make a function that can
Unfortunately I am not writing this question from my Developing PC so I might
I was writing this code on codeblocks(only compiler I m having).When I encountered such
I was writing this code: public static void main(String[] args) { double g =
I'm not actually writing this software myself, but it occurred to me that I
I started writing this algorithm: public static String convert(String str) { if (str.equals(# ))
So I've been writing this tool and I am using these Sockets (not these
I am writing this app in windows form c#. When user exit from this
i am writing this code: awk -F'=' '!/^$/{arr[$1]=$2}END{for (x in arr) {print x=arr[x]}}' 1.txt
I am having trouble writing this query. I need to get the current number

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.