//typescript
let a: string = 'hello2222';
console.log(a);
//枚舉
enum Color{Red, Green, Blue};
let c: Color = Color.Green;
let colorName: string = Color[2];
console.log('color', c, colorName);
let notSure: any = 4;
notSure.toFixed();
let prettySure: Object = 4;
// prettySure.toFixed(); //error
// for (let i = 0; i < 10; i++) {
// setTimeout(function() { console.log(i); }, 100 * i);
// }
let [, second, , fourth] = [1, 2, 3, 4];
console.log(second, fourth); //2 ,4
let o = {
x: "foo",
b: 12,
c: "bar"
};
let { x, ...passthrough } = o;
let total = passthrough.b + passthrough.c.length;
console.log(total);
let {x: foostr, c: barstr} = o;
console.log(foostr, barstr); // <==> var foostr = o.x; var barstr = o.c;
//展開(kāi)對(duì)象
let sss = { y: 'hhh', z: 'eeee'};
let bothstr = {...sss, z: '8888'};
console.log(bothstr); //展開(kāi)對(duì)象后面的屬性會(huì)覆蓋前面的屬性
//Typescript的核心原則之一是對(duì)值所具有的結(jié)構(gòu)進(jìn)行類(lèi)型檢查
function ducktyping(duckstr: {id: string; value?: any}){
duckstr.value = 'ducktypingssssss';
console.log(duckstr.id);
}
var d = {id: 'ducktyping'};
ducktyping(d);
//interface的使用
interface LabelledValue {
size?: number;
label?: string; //帶有問(wèn)號(hào)表示可選屬性
readonly value: number; //只讀屬性
}
function printLabel(labelledObj: LabelledValue){
if(!labelledObj.label){
labelledObj.label = 'hhhhha';
}
// labelledObj.value = 999; //error
console.log(labelledObj.size, labelledObj.label);
}
// let myObj = {size: 10, label:'size 10 Object'};
let myObj = {size: 10, value: 998};
printLabel(myObj);
interface Point {
readonly x: number;
readonly y: number;
}
let p1:Point = {x: 10, y:10};
// p1.y = 20; //error y is a read-only property
//ReadonlyArray<T>
let axxx: number[] = [1,2,3,4,5,6];
let ro: ReadonlyArray<number> = axxx;
axxx.push(7);
axxx.length = 10;
console.log(axxx);
// ro.push(8); //error
// ro.length = 10; //error
//如何判斷該使用readonly或者const, 作為變量使用用const,作為屬性使用則用readonly
//額外的屬性檢查
interface Squareconfig{
width?: string;
height?: string;
[propName: string]: any;
}
function createSquare(config: Squareconfig):{area: string}{
// let newConfig = {width: "1111", area: "9999"};
// if(config.width){
// newConfig.width = config.width;
// }
// return newConfig;
let newConfig = {...config, area: "100"};
return newConfig; //return 不存在的屬性不能分配
}
// createSquare({widtheee: "9999"} as Squareconfig);
let squareOptions = {widtheee: "9999xxx"};
let mySquare = createSquare(squareOptions);
console.log(mySquare);
//函數(shù)類(lèi)型
interface SearchFunc{
(source: string, substring: string): boolean
}
let mySearch: SearchFunc;
mySearch = function(src: string, sub: string): boolean{
return true;
}
//可索引的類(lèi)型
//類(lèi)類(lèi)型
//繼承接口
interface Shape {
color: string
}
interface PenStroke {
penWidth: number;
}
interface Square extends Shape, PenStroke{
sideLength: number;
}
let square = <Square>{};
square.color = "#ff0000";
square.sideLength = 10;
square.penWidth = 5.0;
//混合類(lèi)型
//接口繼承類(lèi)
2019-10-29 Typescript的基礎(chǔ)數(shù)據(jù)類(lèi)型和接口
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。