Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
log
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Eddy Guo
log
Commits
c1ef6fa7
Commit
c1ef6fa7
authored
Sep 03, 2017
by
Eddy Guo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add typescript
parent
3d4198c8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
193 additions
and
0 deletions
+193
-0
03.md
2017:09/03.md
+179
-0
ssr.render.log
ssr.render.log
+0
-0
test.js
test.js
+4
-0
test.ts
test.ts
+10
-0
No files found.
2017:09/03.md
0 → 100644
View file @
c1ef6fa7
# TypeScript
## varibale
```
js
// boolean
let
createdByNewBoolean
:
Boolean
=
new
Boolean
(
1
);
let
createdByBoolean
:
boolean
=
Boolean
(
1
);
// number
let
decLiteral
:
number
=
6
;
let
octalLiteral
:
number
=
0o744
;
// 八进制
var
notANumber
=
NaN
;
// string
let
myName
:
string
=
'test'
// undefined and null
let
u
:
undefined
=
undefined
;
let
n
:
null
=
null
;
let
num
:
number
=
undefined
;
// 为其他类型的子类型
// any 允许被赋值为任何类型, default
let
myFavoriteNumber
:
any
=
'seven'
;
myFavoriteNumber
=
7
;
// union types
let
myFavoriteNumber
:
string
|
number
;
```
## interfaces
```
js
interface
Person
{
readonly
id
:
number
;
// 只能在创建的时候被赋值,不能再改了
name
:
string
;
age
?:
number
;
// ? 可选属性
[
propName
:
string
]:
any
;
// 任意属性,(确定属性及可选属性需为其子属性)
}
let
gy
:
Person
{
name
:
'gy'
,
age
?:
23
,
}
```
## array
```
js
let
fibonacci
:
number
[]
=
[
1
,
1
,
3
];
let
strnacci
:
string
[]
=
[
'1'
,
'2'
,
'3'
];
// Generic 范型
let
fib
:
Array
<
number
>
=
[
1
,
2
,
3
,
4
,
5
];
// use Interface
interface
NumberArr
{
[
index
:
number
]:
number
}
let
test
:
NumberArr
=
[
1
,
2
,
3
,
4
,
5
];
// 任意类型
let
list
:
any
[]
=
[
'test'
,
1
];
```
## function
```
js
// function declaration
function
sum
(
x
:
number
,
y
:
number
):
number
{
return
x
+
y
;
}
// function expression
var
sum
:
(
x
:
number
,
y
:
number
)
=>
number
=
function
(
x
:
number
,
y
:
number
):
number
{
return
x
+
y
;
}
// or use interface
interface
SearchFuc
{
(
source
:
string
,
subString
:
string
):
boolean
;
}
let
search
:
SearchFuc
;
search
=
function
(
source
:
string
,
subString
:
string
)
{
return
source
.
search
(
subString
)
!==
-
1
;
}
// 可选参数
function
sum
(
a
:
number
,
b
:
number
,
c
?:
number
)
{
if
(
c
)
{
return
a
+
b
+
c
;
}
return
a
+
b
;
}
```
## assert
```
js
// 类型断言
<
类型
>
值
值
as
类型
```
## 声明文件
```
js
// 当使用第三方库时,我们需要引用它的声明文件,通常将其所有声明文件放入单独的文件中
// jQuery.d.ts
declare
var
jQuery
:
(
string
)
=>
any
;
// 在使用的文件中,使用三「三斜线」 表明引用的声明文件
/// <reference path='./jQuery.d.ts'></reference>
// 使用工具
@
types
npm
install
@
types
/jquery --save-de
v
```
## 内置对象
```
js
// Boolean, Error, Date, RegExp
let
b
:
Boolean
=
new
Boolean
(
1
);
let
e
:
Error
=
new
Error
(
'Error occurred'
);
let
d
:
Date
=
new
Date
();
let
r
:
RegExp
=
/
[
a-z
]
/
;
// DOM and BOM
Document
,
HTMLElement
,
Event
,
NodeList
let
body
:
HTMLElement
=
document
.
body
;
let
allDiv
:
NodeList
=
document
.
querySelectorAll
(
'div'
);
document
.
addEventListener
(
'click'
,
function
(
e
:
MouseEvent
))
{
// do sth
}
```
## 类型别名
```
js
type
Name
=
string
;
type
NameResolver
=
()
=>
string
;
type
NameOrResolver
=
Name
|
NameResolver
;
function
getName
(
n
:
NameOrResolver
):
Name
{
if
(
typeof
n
===
'string'
)
{
return
n
}
else
{
return
n
();
}
}
```
## 字符串字面量类型
```
js
type
EventNames
=
'click'
|
'scroll'
|
'mousemover'
;
function
handleEvent
(
ele
:
Element
,
event
:
EventNames
)
{
// do sth
}
handleEvent
(
ele
,
'scroll'
);
handleEvent
(
ele
,
'click'
);
```
## enum
```
js
enum
Days
{
a
,
b
,
c
,
d
,
e
,
f
,
g
}
```
\ No newline at end of file
ssr.render.log
deleted
100644 → 0
View file @
3d4198c8
test.js
0 → 100644
View file @
c1ef6fa7
var
gy
=
{
name
:
'gy'
,
age
:
12
};
test.ts
0 → 100644
View file @
c1ef6fa7
interface
Person
{
name
:
string
;
age
:
number
;
}
let
gy
:
Person
=
{
name
:
'gy'
,
age
:
12
,
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment