Test for array of string type in TypeScript












49















How can I test if a variable is an array of string in TypeScript? Something like this:



function f(): string {
var a: string = ["A", "B", "C"];

if (typeof a === "string") {
return "Yes"
}
else {
// returns no as it's 'object'
return "No"
}
};


TypeScript.io here: http://typescript.io/k0ZiJzso0Qg/2



Edit: I've updated the text to ask for a test for string. This was only in the code example previously.










share|improve this question

























  • possible duplicate of How do you check if a variable is an array in JavaScript?

    – WiredPrairie
    Apr 17 '14 at 10:48











  • As TypeScript just compiles to JavaScript, the answers may be found by searching for a JavaScript solution. Further, it's worth it to see some of the answers as the answer depends on the host and how it's being used and passed.

    – WiredPrairie
    Apr 17 '14 at 10:50


















49















How can I test if a variable is an array of string in TypeScript? Something like this:



function f(): string {
var a: string = ["A", "B", "C"];

if (typeof a === "string") {
return "Yes"
}
else {
// returns no as it's 'object'
return "No"
}
};


TypeScript.io here: http://typescript.io/k0ZiJzso0Qg/2



Edit: I've updated the text to ask for a test for string. This was only in the code example previously.










share|improve this question

























  • possible duplicate of How do you check if a variable is an array in JavaScript?

    – WiredPrairie
    Apr 17 '14 at 10:48











  • As TypeScript just compiles to JavaScript, the answers may be found by searching for a JavaScript solution. Further, it's worth it to see some of the answers as the answer depends on the host and how it's being used and passed.

    – WiredPrairie
    Apr 17 '14 at 10:50
















49












49








49


4






How can I test if a variable is an array of string in TypeScript? Something like this:



function f(): string {
var a: string = ["A", "B", "C"];

if (typeof a === "string") {
return "Yes"
}
else {
// returns no as it's 'object'
return "No"
}
};


TypeScript.io here: http://typescript.io/k0ZiJzso0Qg/2



Edit: I've updated the text to ask for a test for string. This was only in the code example previously.










share|improve this question
















How can I test if a variable is an array of string in TypeScript? Something like this:



function f(): string {
var a: string = ["A", "B", "C"];

if (typeof a === "string") {
return "Yes"
}
else {
// returns no as it's 'object'
return "No"
}
};


TypeScript.io here: http://typescript.io/k0ZiJzso0Qg/2



Edit: I've updated the text to ask for a test for string. This was only in the code example previously.







typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 17 '14 at 11:22







Sean Kearon

















asked Apr 17 '14 at 10:11









Sean KearonSean Kearon

5,01595983




5,01595983













  • possible duplicate of How do you check if a variable is an array in JavaScript?

    – WiredPrairie
    Apr 17 '14 at 10:48











  • As TypeScript just compiles to JavaScript, the answers may be found by searching for a JavaScript solution. Further, it's worth it to see some of the answers as the answer depends on the host and how it's being used and passed.

    – WiredPrairie
    Apr 17 '14 at 10:50





















  • possible duplicate of How do you check if a variable is an array in JavaScript?

    – WiredPrairie
    Apr 17 '14 at 10:48











  • As TypeScript just compiles to JavaScript, the answers may be found by searching for a JavaScript solution. Further, it's worth it to see some of the answers as the answer depends on the host and how it's being used and passed.

    – WiredPrairie
    Apr 17 '14 at 10:50



















possible duplicate of How do you check if a variable is an array in JavaScript?

– WiredPrairie
Apr 17 '14 at 10:48





possible duplicate of How do you check if a variable is an array in JavaScript?

– WiredPrairie
Apr 17 '14 at 10:48













As TypeScript just compiles to JavaScript, the answers may be found by searching for a JavaScript solution. Further, it's worth it to see some of the answers as the answer depends on the host and how it's being used and passed.

– WiredPrairie
Apr 17 '14 at 10:50







As TypeScript just compiles to JavaScript, the answers may be found by searching for a JavaScript solution. Further, it's worth it to see some of the answers as the answer depends on the host and how it's being used and passed.

– WiredPrairie
Apr 17 '14 at 10:50














6 Answers
6






active

oldest

votes


















98














You cannot test for string in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330



If you specifically want for string array you can do something like:



if (value instanceof Array) {
var somethingIsNotString = false;
value.forEach(function(item){
if(typeof item !== 'string'){
somethingIsNotString = true;
}
})
if(!somethingIsNotString && value.length > 0){
console.log('string!');
}
}





share|improve this answer


























  • Thanks - that's what I needed!

    – Sean Kearon
    Apr 17 '14 at 13:42



















30














Another option is Array.isArray()



if(! Array.isArray(classNames) ){
classNames = [classNames]
}





share|improve this answer





















  • 1





    But this doesn't check for the elements being of type string.

    – Ixx
    Jan 8 at 11:13






  • 1





    Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

    – grigson
    Jan 9 at 15:21













  • @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

    – giovannipds
    Jan 17 at 11:35





















6














Here is the most concise solution so far:



function isArrayOfString(value: any): boolean {
return Array.isArray(value) && value.every(item => typeof item === "string")
}


Note that value.every will return true for an empty array. If you need to return false for an empty array, you should add !!value.length to the condition clause:



function isNonEmptyArrayOfStrings(value: any): boolean {
return Array.isArray(value) && !!value.length && value.every(item => typeof item === "string");
}


There is no way to check the type of an empty array since there is no type information at runtime.






share|improve this answer

































    3














    I know this has been answered, but TypeScript introduced type guards: https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards



    If you have a type like: Object | string and what to do something conditionally based on what type it is - you can use this type guarding:



    function isStringArray(value: any): value is string {
    if (value instanceof Array) {
    value.forEach(function(item) { // maybe only check first value?
    if (typeof item !== 'string') {
    return false
    }
    })
    return true
    }
    return false
    }

    function join<T>(value: string | T) {
    if (isStringArray(value)) {
    return value.join(',') // value is string here
    } else {
    return value.map((x) => x.toString()).join(',') // value is T here
    }
    }


    There is an issue with an empty array being typed as string, but that might be okay






    share|improve this answer



















    • 6





      The return false in the forEach has no effect.

      – Ishtar
      May 9 '18 at 8:39



















    0














    Try this:



    if (value instanceof Array) {
    alert('value is Array!');
    } else {
    alert('Not an array');
    }





    share|improve this answer



















    • 5





      Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

      – WiredPrairie
      Apr 17 '14 at 10:49













    • This just check the main type not every Array's child.

      – giovannipds
      Jan 17 at 11:39



















    0














    there is a little problem here because the



    if (typeof item !== 'string') {
    return false
    }


    will not stop the foreach.
    So the function will return true even if the array does contain none string values.



    This seems to wok for me:



    function isStringArray(value: any): value is number {
    if (Object.prototype.toString.call(value) === '[object Array]') {
    if (value.length < 1) {
    return false;
    } else {
    return value.every((d: any) => typeof d === 'string');
    }
    }
    return false;
    }


    Greetings, Hans






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23130292%2ftest-for-array-of-string-type-in-typescript%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      98














      You cannot test for string in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330



      If you specifically want for string array you can do something like:



      if (value instanceof Array) {
      var somethingIsNotString = false;
      value.forEach(function(item){
      if(typeof item !== 'string'){
      somethingIsNotString = true;
      }
      })
      if(!somethingIsNotString && value.length > 0){
      console.log('string!');
      }
      }





      share|improve this answer


























      • Thanks - that's what I needed!

        – Sean Kearon
        Apr 17 '14 at 13:42
















      98














      You cannot test for string in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330



      If you specifically want for string array you can do something like:



      if (value instanceof Array) {
      var somethingIsNotString = false;
      value.forEach(function(item){
      if(typeof item !== 'string'){
      somethingIsNotString = true;
      }
      })
      if(!somethingIsNotString && value.length > 0){
      console.log('string!');
      }
      }





      share|improve this answer


























      • Thanks - that's what I needed!

        – Sean Kearon
        Apr 17 '14 at 13:42














      98












      98








      98







      You cannot test for string in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330



      If you specifically want for string array you can do something like:



      if (value instanceof Array) {
      var somethingIsNotString = false;
      value.forEach(function(item){
      if(typeof item !== 'string'){
      somethingIsNotString = true;
      }
      })
      if(!somethingIsNotString && value.length > 0){
      console.log('string!');
      }
      }





      share|improve this answer















      You cannot test for string in the general case but you can test for Array quite easily the same as in JavaScript https://stackoverflow.com/a/767492/390330



      If you specifically want for string array you can do something like:



      if (value instanceof Array) {
      var somethingIsNotString = false;
      value.forEach(function(item){
      if(typeof item !== 'string'){
      somethingIsNotString = true;
      }
      })
      if(!somethingIsNotString && value.length > 0){
      console.log('string!');
      }
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 15 at 11:14









      Nedim Kurbegović

      5210




      5210










      answered Apr 17 '14 at 12:53









      basaratbasarat

      140k26260369




      140k26260369













      • Thanks - that's what I needed!

        – Sean Kearon
        Apr 17 '14 at 13:42



















      • Thanks - that's what I needed!

        – Sean Kearon
        Apr 17 '14 at 13:42

















      Thanks - that's what I needed!

      – Sean Kearon
      Apr 17 '14 at 13:42





      Thanks - that's what I needed!

      – Sean Kearon
      Apr 17 '14 at 13:42













      30














      Another option is Array.isArray()



      if(! Array.isArray(classNames) ){
      classNames = [classNames]
      }





      share|improve this answer





















      • 1





        But this doesn't check for the elements being of type string.

        – Ixx
        Jan 8 at 11:13






      • 1





        Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

        – grigson
        Jan 9 at 15:21













      • @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

        – giovannipds
        Jan 17 at 11:35


















      30














      Another option is Array.isArray()



      if(! Array.isArray(classNames) ){
      classNames = [classNames]
      }





      share|improve this answer





















      • 1





        But this doesn't check for the elements being of type string.

        – Ixx
        Jan 8 at 11:13






      • 1





        Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

        – grigson
        Jan 9 at 15:21













      • @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

        – giovannipds
        Jan 17 at 11:35
















      30












      30








      30







      Another option is Array.isArray()



      if(! Array.isArray(classNames) ){
      classNames = [classNames]
      }





      share|improve this answer















      Another option is Array.isArray()



      if(! Array.isArray(classNames) ){
      classNames = [classNames]
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 13 '17 at 3:17









      daremachine

      1,40121528




      1,40121528










      answered Dec 26 '16 at 12:06









      grigsongrigson

      1,9561914




      1,9561914








      • 1





        But this doesn't check for the elements being of type string.

        – Ixx
        Jan 8 at 11:13






      • 1





        Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

        – grigson
        Jan 9 at 15:21













      • @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

        – giovannipds
        Jan 17 at 11:35
















      • 1





        But this doesn't check for the elements being of type string.

        – Ixx
        Jan 8 at 11:13






      • 1





        Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

        – grigson
        Jan 9 at 15:21













      • @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

        – giovannipds
        Jan 17 at 11:35










      1




      1





      But this doesn't check for the elements being of type string.

      – Ixx
      Jan 8 at 11:13





      But this doesn't check for the elements being of type string.

      – Ixx
      Jan 8 at 11:13




      1




      1





      Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

      – grigson
      Jan 9 at 15:21







      Yeap. Then you need one additional checking. Array.isArray(classNames) && classNames.every(it => typeof it === 'string')

      – grigson
      Jan 9 at 15:21















      @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

      – giovannipds
      Jan 17 at 11:35







      @grigson how about performance when checking 400+ results? Should I disconsider this kind of type check, check just the first child or is it safe tu use this "every" thing?

      – giovannipds
      Jan 17 at 11:35













      6














      Here is the most concise solution so far:



      function isArrayOfString(value: any): boolean {
      return Array.isArray(value) && value.every(item => typeof item === "string")
      }


      Note that value.every will return true for an empty array. If you need to return false for an empty array, you should add !!value.length to the condition clause:



      function isNonEmptyArrayOfStrings(value: any): boolean {
      return Array.isArray(value) && !!value.length && value.every(item => typeof item === "string");
      }


      There is no way to check the type of an empty array since there is no type information at runtime.






      share|improve this answer






























        6














        Here is the most concise solution so far:



        function isArrayOfString(value: any): boolean {
        return Array.isArray(value) && value.every(item => typeof item === "string")
        }


        Note that value.every will return true for an empty array. If you need to return false for an empty array, you should add !!value.length to the condition clause:



        function isNonEmptyArrayOfStrings(value: any): boolean {
        return Array.isArray(value) && !!value.length && value.every(item => typeof item === "string");
        }


        There is no way to check the type of an empty array since there is no type information at runtime.






        share|improve this answer




























          6












          6








          6







          Here is the most concise solution so far:



          function isArrayOfString(value: any): boolean {
          return Array.isArray(value) && value.every(item => typeof item === "string")
          }


          Note that value.every will return true for an empty array. If you need to return false for an empty array, you should add !!value.length to the condition clause:



          function isNonEmptyArrayOfStrings(value: any): boolean {
          return Array.isArray(value) && !!value.length && value.every(item => typeof item === "string");
          }


          There is no way to check the type of an empty array since there is no type information at runtime.






          share|improve this answer















          Here is the most concise solution so far:



          function isArrayOfString(value: any): boolean {
          return Array.isArray(value) && value.every(item => typeof item === "string")
          }


          Note that value.every will return true for an empty array. If you need to return false for an empty array, you should add !!value.length to the condition clause:



          function isNonEmptyArrayOfStrings(value: any): boolean {
          return Array.isArray(value) && !!value.length && value.every(item => typeof item === "string");
          }


          There is no way to check the type of an empty array since there is no type information at runtime.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 2 at 8:49

























          answered May 25 '18 at 7:08









          asmironovasmironov

          520616




          520616























              3














              I know this has been answered, but TypeScript introduced type guards: https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards



              If you have a type like: Object | string and what to do something conditionally based on what type it is - you can use this type guarding:



              function isStringArray(value: any): value is string {
              if (value instanceof Array) {
              value.forEach(function(item) { // maybe only check first value?
              if (typeof item !== 'string') {
              return false
              }
              })
              return true
              }
              return false
              }

              function join<T>(value: string | T) {
              if (isStringArray(value)) {
              return value.join(',') // value is string here
              } else {
              return value.map((x) => x.toString()).join(',') // value is T here
              }
              }


              There is an issue with an empty array being typed as string, but that might be okay






              share|improve this answer



















              • 6





                The return false in the forEach has no effect.

                – Ishtar
                May 9 '18 at 8:39
















              3














              I know this has been answered, but TypeScript introduced type guards: https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards



              If you have a type like: Object | string and what to do something conditionally based on what type it is - you can use this type guarding:



              function isStringArray(value: any): value is string {
              if (value instanceof Array) {
              value.forEach(function(item) { // maybe only check first value?
              if (typeof item !== 'string') {
              return false
              }
              })
              return true
              }
              return false
              }

              function join<T>(value: string | T) {
              if (isStringArray(value)) {
              return value.join(',') // value is string here
              } else {
              return value.map((x) => x.toString()).join(',') // value is T here
              }
              }


              There is an issue with an empty array being typed as string, but that might be okay






              share|improve this answer



















              • 6





                The return false in the forEach has no effect.

                – Ishtar
                May 9 '18 at 8:39














              3












              3








              3







              I know this has been answered, but TypeScript introduced type guards: https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards



              If you have a type like: Object | string and what to do something conditionally based on what type it is - you can use this type guarding:



              function isStringArray(value: any): value is string {
              if (value instanceof Array) {
              value.forEach(function(item) { // maybe only check first value?
              if (typeof item !== 'string') {
              return false
              }
              })
              return true
              }
              return false
              }

              function join<T>(value: string | T) {
              if (isStringArray(value)) {
              return value.join(',') // value is string here
              } else {
              return value.map((x) => x.toString()).join(',') // value is T here
              }
              }


              There is an issue with an empty array being typed as string, but that might be okay






              share|improve this answer













              I know this has been answered, but TypeScript introduced type guards: https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards



              If you have a type like: Object | string and what to do something conditionally based on what type it is - you can use this type guarding:



              function isStringArray(value: any): value is string {
              if (value instanceof Array) {
              value.forEach(function(item) { // maybe only check first value?
              if (typeof item !== 'string') {
              return false
              }
              })
              return true
              }
              return false
              }

              function join<T>(value: string | T) {
              if (isStringArray(value)) {
              return value.join(',') // value is string here
              } else {
              return value.map((x) => x.toString()).join(',') // value is T here
              }
              }


              There is an issue with an empty array being typed as string, but that might be okay







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 7 '17 at 20:46









              Nicholas BollNicholas Boll

              42525




              42525








              • 6





                The return false in the forEach has no effect.

                – Ishtar
                May 9 '18 at 8:39














              • 6





                The return false in the forEach has no effect.

                – Ishtar
                May 9 '18 at 8:39








              6




              6





              The return false in the forEach has no effect.

              – Ishtar
              May 9 '18 at 8:39





              The return false in the forEach has no effect.

              – Ishtar
              May 9 '18 at 8:39











              0














              Try this:



              if (value instanceof Array) {
              alert('value is Array!');
              } else {
              alert('Not an array');
              }





              share|improve this answer



















              • 5





                Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

                – WiredPrairie
                Apr 17 '14 at 10:49













              • This just check the main type not every Array's child.

                – giovannipds
                Jan 17 at 11:39
















              0














              Try this:



              if (value instanceof Array) {
              alert('value is Array!');
              } else {
              alert('Not an array');
              }





              share|improve this answer



















              • 5





                Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

                – WiredPrairie
                Apr 17 '14 at 10:49













              • This just check the main type not every Array's child.

                – giovannipds
                Jan 17 at 11:39














              0












              0








              0







              Try this:



              if (value instanceof Array) {
              alert('value is Array!');
              } else {
              alert('Not an array');
              }





              share|improve this answer













              Try this:



              if (value instanceof Array) {
              alert('value is Array!');
              } else {
              alert('Not an array');
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 17 '14 at 10:41









              TcanarchyTcanarchy

              454417




              454417








              • 5





                Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

                – WiredPrairie
                Apr 17 '14 at 10:49













              • This just check the main type not every Array's child.

                – giovannipds
                Jan 17 at 11:39














              • 5





                Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

                – WiredPrairie
                Apr 17 '14 at 10:49













              • This just check the main type not every Array's child.

                – giovannipds
                Jan 17 at 11:39








              5




              5





              Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

              – WiredPrairie
              Apr 17 '14 at 10:49







              Rather than copying another answer, please just point out the duplicate as a comment, especially as this doesn't work in all cases.

              – WiredPrairie
              Apr 17 '14 at 10:49















              This just check the main type not every Array's child.

              – giovannipds
              Jan 17 at 11:39





              This just check the main type not every Array's child.

              – giovannipds
              Jan 17 at 11:39











              0














              there is a little problem here because the



              if (typeof item !== 'string') {
              return false
              }


              will not stop the foreach.
              So the function will return true even if the array does contain none string values.



              This seems to wok for me:



              function isStringArray(value: any): value is number {
              if (Object.prototype.toString.call(value) === '[object Array]') {
              if (value.length < 1) {
              return false;
              } else {
              return value.every((d: any) => typeof d === 'string');
              }
              }
              return false;
              }


              Greetings, Hans






              share|improve this answer






























                0














                there is a little problem here because the



                if (typeof item !== 'string') {
                return false
                }


                will not stop the foreach.
                So the function will return true even if the array does contain none string values.



                This seems to wok for me:



                function isStringArray(value: any): value is number {
                if (Object.prototype.toString.call(value) === '[object Array]') {
                if (value.length < 1) {
                return false;
                } else {
                return value.every((d: any) => typeof d === 'string');
                }
                }
                return false;
                }


                Greetings, Hans






                share|improve this answer




























                  0












                  0








                  0







                  there is a little problem here because the



                  if (typeof item !== 'string') {
                  return false
                  }


                  will not stop the foreach.
                  So the function will return true even if the array does contain none string values.



                  This seems to wok for me:



                  function isStringArray(value: any): value is number {
                  if (Object.prototype.toString.call(value) === '[object Array]') {
                  if (value.length < 1) {
                  return false;
                  } else {
                  return value.every((d: any) => typeof d === 'string');
                  }
                  }
                  return false;
                  }


                  Greetings, Hans






                  share|improve this answer















                  there is a little problem here because the



                  if (typeof item !== 'string') {
                  return false
                  }


                  will not stop the foreach.
                  So the function will return true even if the array does contain none string values.



                  This seems to wok for me:



                  function isStringArray(value: any): value is number {
                  if (Object.prototype.toString.call(value) === '[object Array]') {
                  if (value.length < 1) {
                  return false;
                  } else {
                  return value.every((d: any) => typeof d === 'string');
                  }
                  }
                  return false;
                  }


                  Greetings, Hans







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 14 '17 at 5:24

























                  answered Sep 11 '17 at 22:31









                  hanshans

                  819914




                  819914






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23130292%2ftest-for-array-of-string-type-in-typescript%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas