Repository: microsoft/kiota (csharp)

Issue:
Use primitive type in TS
### What are you generating using Kiota, clients or plugins?

API Client/SDK

### In what context or format are you using Kiota?

Nuget tool

### Client library/SDK language

TypeScript

### Describe the bug

Some type constants in the Typescript builder point to non-primitive types.

https://github.com/microsoft/kiota/blob/01daa5215c47dddc892d9b59a48d9f687947b9de/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs#L27-L29 

The `String` | `Object` | `Boolean` types are wrapper objects for primitive types. So if we try to use a property with the wrapper type inside a function that requires the primitive type. It produces an error that can be avoided.


If a OpenAPI schema has a property of type `base64url`, it will be translated to `String` instead of `string`
https://github.com/microsoft/kiota/blob/01daa5215c47dddc892d9b59a48d9f687947b9de/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs#L208

### Expected behavior

If we follow the Typescript documentation, we need to use the primitive types.

https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#general-types

This will avoid TS errors when we try to use a value of type `String` where a type `string` is required.

playground: https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAhgLhgZSgJwJZgOYwLwwBEchAUKQGYCuYwUG4MAtgJ4BiNwAFHGtktEw4AlDADepGFJhoAplCpow8PqQC+5Vh1o9hpIA

### How to reproduce

Use an openapi description with a property that has `type: string` and `format: base64url`.
Generate a client in Typescript.



### Open API description file

```yaml
openapi: 3.0.0
info:
  title: Test
  version: '1'
tags: []
paths:
  /test:
    get:
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Test'
components:
  schemas:
    Test:
      type: object
      properties:
        imBase64:
          type: string
          format: base64url
servers:
  - url: https://example.com
    description: Example server
```

### Kiota Version

1.23.0+e148ba500009faf72a57883f905e1ca922add0e2

### Latest Kiota version known to work for scenario above?(Not required)

_No response_

### Known Workarounds

Manually update the definition inside the model or use `as string`.

But this can be easily fixed if we avoid using the wrapper type in the writer.

### Configuration

- OS: Windows 11
- architecture: x64

Not relevant here.

### Debug output

Not relevant.

### Other information

The following constants can be rewritten using the primitive types.

https://github.com/microsoft/kiota/blob/01daa5215c47dddc892d9b59a48d9f687947b9de/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs#L27-L29 

I don't know why there is a difference between `TYPE_STRING` and `TYPE_LOWERCASE_STRING`. I assume that using only primitives won't break anything

Pull request:
Resolves https://github.com/microsoft/kiota/issues/6124

Binary data types in TypeScript are now correctly mapped to `ArrayBuffer` instead of `String`.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3a27a4f9c3..f65217252f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Changed
 
+- Fixed mapping of Binary Types to array buffer in TypeScript. [#6124](https://github.com/microsoft/kiota/issues/6124)
+
 ## [1.23.0] - 2025-02-06
 
 ### Added
@@ -20,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Added support for net9 only installations of the dotnet tool. [#5887](https://github.com/microsoft/kiota/issues/5887)
 
 ### Changed
+
 - Fixes serialization of collection of enums. [microsoft/kiota-typescript#1276](https://github.com/microsoft/kiota-typescript/issues/1276)
 
 ## [1.22.3]
@@ -1568,4 +1571,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 - Initial GitHub release
 
-
+
diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs
index a963796ba6..8a306b877b 100644
--- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs
+++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs
@@ -205,7 +205,7 @@ public static string TranslateTypescriptType(CodeTypeBase type)
         return type?.Name switch
         {
             TYPE_INTEGER or TYPE_INT or TYPE_INT64 or TYPE_FLOAT or TYPE_DOUBLE or TYPE_BYTE or TYPE_SBYTE or TYPE_DECIMAL => TYPE_NUMBER,
-            TYPE_BINARY or TYPE_BASE64 or TYPE_BASE64URL => TYPE_STRING,
+            TYPE_BINARY or TYPE_BASE64 or TYPE_BASE64URL => "ArrayBuffer",
             TYPE_STRING or TYPE_OBJECT or TYPE_BOOLEAN or TYPE_VOID or TYPE_LOWERCASE_STRING or TYPE_LOWERCASE_OBJECT or TYPE_LOWERCASE_BOOLEAN or TYPE_LOWERCASE_VOID => type.Name.ToFirstCharacterLowerCase(),
             null => TYPE_OBJECT,
             _ when type is CodeComposedTypeBase composedType => composedType.Name.ToFirstCharacterUpperCase(),


Test patch:
diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodePropertyWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodePropertyWriterTests.cs
index 99eb614fd9..43ba825033 100644
--- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodePropertyWriterTests.cs
+++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodePropertyWriterTests.cs
@@ -123,4 +123,16 @@ public void FailsOnPropertiesForClasses()
         };
         Assert.Throws<InvalidOperationException>(() => writer.Write(property));
     }
+    [Fact]
+    public void WritesCorrectTypeForProperty()
+    {
+        property.Kind = CodePropertyKind.Custom;
+        property.Type = new CodeType
+        {
+            Name = "base64"
+        };
+        writer.Write(property);
+        var result = tw.ToString();
+        Assert.Contains($"{PropertyName}?: ArrayBuffer | null", result);
+    }
 }