web-dev-qa-db-fra.com

Transtypage d'un nombre en chaîne dans TypeScript

Quel est le meilleur moyen (s’il en existe un) de convertir un nombre en chaînes en typescript?

var page_number:number = 3;
window.location.hash = page_number; 

Dans ce cas, le compilateur renvoie l'erreur:

Le type 'numéro' n'est pas assignable au type 'chaîne'

Parce que location.hash est une chaîne.

window.location.hash = ""+page_number; //casting using "" literal
window.location.hash = String(number); //casting creating using the String() function

Alors, quelle méthode est la meilleure?

132
Ma Jerez

"Casting" est différent de la conversion. Dans ce cas, window.location.hash convertira automatiquement un nombre en chaîne. Mais pour éviter une erreur de compilation TypeScript, vous pouvez effectuer la conversion de chaîne vous-même:

window.location.hash = ""+page_number; 
window.location.hash = String(page_number); 

Ces conversions sont idéales si vous ne souhaitez pas qu'une erreur soit générée lorsque page_number est null ou undefined. Alors que page_number.toString() et page_number.toLocaleString() lancera lorsque page_number sera null ou undefined.

Lorsque vous avez seulement besoin de transtyper, pas de convertir, voici comment transtyper en chaîne dans TypeScript:

window.location.hash = <string>page_number; 
// or 
window.location.hash = page_number as string;

Les annotations de conversion <string> ou as string indiquent au compilateur TypeScript de traiter page_number comme une chaîne au moment de la compilation; il ne convertit pas au moment de l'exécution.

Cependant, le compilateur se plaindra que vous ne pouvez pas affecter un nombre à une chaîne. Vous devez commencer par transtyper sur <any>, puis sur <string>:

window.location.hash = <string><any>page_number;
// or
window.location.hash = page_number as any as string;

Il est donc plus facile de convertir, qui gère le type au moment de l'exécution et de la compilation:

window.location.hash = String(page_number); 

(Merci à @RuslanPolutsygan pour avoir détecté le problème de la diffusion du numéro de chaîne.)

233
Robert Penner

Il suffit d'utiliser toString ou toLocaleString je dirais. Alors:

var page_number:number = 3;
window.location.hash = page_number.toLocaleString();

Celles-ci génèrent une erreur si page_number est null ou undefined. Si vous ne le souhaitez pas, vous pouvez choisir le correctif adapté à votre situation:

// Fix 1:
window.location.hash = (page_number || 1).toLocaleString();

// Fix 2a:
window.location.hash = !page_number ? "1" page_number.toLocaleString();

// Fix 2b (allows page_number to be zero):
window.location.hash = (page_number !== 0 && !page_number) ? "1" page_number.toLocaleString();
28
Jeroen

window.location.hash est un string, alors faites ceci:

var page_number: number = 3;
window.location.hash = page_number.toString(); 
3
raneshu

On peut également utiliser la syntaxe suivante dans TypeScript. Notez le backtick "` "

window.location.hash = `${page_number}`
2
Nehal Damania

Utilisez le symbole "+" pour convertir une chaîne en nombre.

window.location.hash = +page_number;
0
Bettaibi Nidhal