<type>函数和函数名称()之间的区别:输入typescript

时间:2017-10-10 12:34:38

标签: typescript

drop procedure proc_name;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_name`(
    IN `x` int, IN `y` int
)

BEGIN 
if( x = y )then
    set @a = 'select 33 as A from dual
union all 
select 44 as A from dual ';
else 
    set @a = 'select 33 as A from dual
union all 
select 55 as A from dual ';
end if;     

    PREPARE stmt FROM @a;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END//
DELIMITER ;

call proc_name(1,0);

请看上面代码中的第2行。为什么我不能function getCounter(): Counter { let counter = <Counter>function (start: number) { }; counter.interval = 123; return counter; }

我想我要问的是function (start: number): Counter<type>

之间的差异

2 个答案:

答案 0 :(得分:0)

:type - 是函数返回值类型的声明。例如:

let counter = function (start: number): number {}
let result = counter(1);

此处,result变量的类型为number

有关详细信息,请参阅function types

<type> - 是type assertion,这意味着它告诉编译器在此断言之后出现的值是指定类型。在您的情况下,变量counter的类型为Counter

答案 1 :(得分:0)

您应该从TypeScript手册中查看此page。 “类型断言”一章解释了<Type>语法。这本书对于学习TypeScript的基础非常有用。

所以基本上:Type语法用于说明变量的类型。例如:

let example: string = "This is a string";
let exampleNumber: number = 12;

但请记住,可以推断出很多TypeScript类型。所以这段代码会有相同的结果:

let example = "This is a string";
let exampleNumber = 12;

有时,TypeScript代码会为您提供any类型。这实际上意味着它可以是任何东西。使用和:Type语法,您将能够像强类型变量一样威胁它。所以代码看起来像:

function test(arg: any): number {
    // Threat the arg variable as string;
    let example: string = arg;
    // Threat the arg variable as string by using Type assertion.
    let otherExample = <string> arg;

    // Returns arg but the return type = :number so the caller will have a number variable.
    return arg;
}
// Nr will be of type number by the compiler.
let nr = test(12);