An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
February 20, 2022 11:13 pm GMT
Original Link: https://dev.to/kaede_io/ts-xing-woshi-su-1nk5
TS --
string number
TypeScript
by SmartHR
===
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
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To