Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent Guid in typescript?

Tags:

let's say I have this C# class

public class Product {    public Guid Id { get; set; }    public string ProductName { get; set; }    public Decimal Price { get; set; }    public int Level { get; set; } } 

The equivalent typescript would be something like:

export class Product {   id: ???;   productName: string;   price: number;   level: number; } 

How to represent Guid in typescript?

like image 661
Richard77 Avatar asked Mar 22 '18 15:03

Richard77


People also ask

How do I pass a GUID in TypeScript?

Guids are usually represented as strings in Javascript, so the simplest way to represent the GUID is as a string. Usually when serialization to JSON occurs it is represented as a string, so using a string will ensure compatibility with data from the server.

Does TypeScript have GUID?

Although neither the TypeScript nor JavaScript language have built-in support for generating a UUID or GUID, there are plenty of quality 3rd party, open-source libraries that you can use.

What is GUID in angular?

Unique Identifier generation is a requirement in any programming language. It contains 128 bits in size separated by a hyphen with 5 groups. Angular is an MVC framework based on Typescript. GUID and UUID generate 128 bits of the implementation.

How is GUID generated?

A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.


2 Answers

Guids are usually represented as strings in Javascript, so the simplest way to represent the GUID is as a string. Usually when serialization to JSON occurs it is represented as a string, so using a string will ensure compatibility with data from the server.

To make the GUID different from a simple string, you could use branded types:

type GUID = string & { isGuid: true}; function guid(guid: string) : GUID {     return  guid as GUID; // maybe add validation that the parameter is an actual guid ? } export interface Product {     id: GUID;     productName: string;     price: number;     level: number; }  declare let p: Product; p.id = "" // error p.id = guid("guid data"); // ok p.id.split('-') // we have access to string methods 

This article has a bit more of a discussion on branded types. Also the typescript compiler uses branded types for paths which is similar to this use case.

like image 189
Titian Cernicova-Dragomir Avatar answered Sep 22 '22 01:09

Titian Cernicova-Dragomir


Another alternative is using following NPM package:

guid-typescript which you can find here: https://www.npmjs.com/package/guid-typescript

Then it will be just like this:

import { Guid } from "guid-typescript";  export class Product {     id: Guid;     productName: string;     price: number;     level: number; } 
like image 22
Jagjit Singh Avatar answered Sep 21 '22 01:09

Jagjit Singh