Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 20, 2022 11:13 pm GMT

TS --

string number

TypeScript

by SmartHR

https://www.amazon.co.jp/%E6%89%8B%E3%82%92%E5%8B%95%E3%81%8B%E3%81%97%E3%81%AA%E3%81%8C%E3%82%89%E5%AD%A6%E3%81%B6-TypeScript-%E6%B8%A1%E9%82%89%E6%AF%94%E5%91%82%E6%A8%B9-ebook/dp/B09KZJXDN1

===

string, number

===

string number object

const dog : {  name: string,  age: number,} = {  name: 'Aki',  age: '300',}

ts object

obj.ts:15:3 - error TS2322: Type 'string' is not assignable to type 'number'.15   age: '300',     ~~~  obj.ts:12:3    12   age: number,         ~~~    The expected type comes from property 'age' which is declared here on type '{ name: string; age: number; }'

type

===

boolean -- '', 0, undefined,

const isOpen: boolean = true

boolean true false

tsc bool.ts        bool.ts:3:7 - error TS2322: Type 'string' is not assignable to type 'boolean'.3 const empty: boolean = ''        ~~~~~bool.ts:4:7 - error TS2322: Type 'number' is not assignable to type 'boolean'.4 const zero: boolean = 0        ~~~~bool.ts:5:7 - error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.5 const undefined: boolean = undefined        ~~~~~~~~~bool.ts:5:28 - error TS2448: Block-scoped variable 'undefined' used before its declaration.5 const undefined: boolean = undefined                             ~~~~~~~~~  bool.ts:5:7    5 const undefined: boolean = undefined            ~~~~~~~~~    'undefined' is declared here.Found 4 errors.

false
ts boolean

===

const list: number[] = [0, 1, 2, ]list.push(99)list.push('text')



tsc arr.ts arr.ts:3:11 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.3 list.push('text')            ~~~~~~


const numList: Array<number> = [1, 2, 3, false, ]

number[] Array

===

null -- strict

const text: string = 'text'const undefString: string = undefinedconst nullString: string = null

undefined null

tsconfig.json/compileOptions/strict/strictNullChecks

{  "compilerOptions": {    "target": "es5",     /* Strict Type-Checking Options */    "strict": true,                                 /* Enable all strict type-checking options. */    "strictNullChecks": true,                    /* Enable strict null checks. */

true ...

let text: string = 'text'text = undefinedtext = null

Image description

strict false null vscode

===

TS

ReadOnly

TS object


const woman : {  readonly name: string,  age: number,} = {  name: 'Aki',  age: 25,}woman.name = 'Teru'


tsc ro.tsro.ts:20:7 - error TS2540: Cannot assign to 'name' because it is a read-only property.20 woman.name = 'Teru'

read-only

===

interface -- object

interface Person {  name: string,  power: number,}const kaede: Person = {  name: 'kaede',  power: 70,}


Original Link: https://dev.to/kaede_io/ts-xing-woshi-su-1nk5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To