除了函数之外,TypeScript 中还有其他类型吗? [英] Is there a type in TypeScript for anything except functions?

查看:28
本文介绍了除了函数之外,TypeScript 中还有其他类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表达一个参数应该是一个对象或一个简单的值类型(数字、布尔值、字符串等),而不是一个函数.

I would like to express that a paremeter should be an object or a simple value type (number, bool, string, etc.), but not a function.

如果我使用Object,编译器会让我分配一个函数.

If I use Object, the compiler let's me to assign a function.

var test: Object = () => "a";

如果我使用any,当然也是同样的结果.在这种情况下,有什么类型或技巧可以帮助我吗?

If I use any, the same result of course too. Is there a type or trick which can help me out in this case?

我的基本目标是在使用 Knockout observables 时保证安全,这样我就不会忘记打开它们的那些小括号:)

My underlying goal is to garantee safety when using Knockout observables, so that I don't forget those little paranthesis to unwrap them :)

推荐答案

New Feature Answer

于 2018 年 11 月添加 - 因为条件类型现在很流行!

New Feature Answer

Added November 2018 - as conditional types are a thing now!

条件类型为此提供了一种可能的解决方案,因为您可以创建一个 NotFunction 条件类型,如下所示:

Conditional types provide a possible solution to this, as you can create a NotFunction conditional type, as shown below:

type NotFunction<T> = T extends Function ? never : T;

其工作原理如下:

const aFunction = (input: string) => input;
const anObject = { data: 'some data' };
const aString = 'data';

// Error function is not a never
const x: NotFunction<typeof aFunction> = aFunction;

// OK
const y: NotFunction<typeof anObject> = anObject;
const z: NotFunction<typeof aString> = aString;

唯一的缺点是你必须将变量放在语句的左侧和右侧 - 尽管如果你犯了这样的错误是安全的:

The only weakness in this is that you have to put the variable on the left side and right side of the statement - although there is safety if you make a mistake such as:

// Error - function is not a string
const x: NotFunction<typeof aString> = aFunction;

原答案

您可以使用 typeof 提供运行时检查,虽然它不是编译时检查,但会捕获您忘记执行函数的那些实例:

Original Answer

You can provide a runtime check using typeof, which although isn't a compile time check will catch those instances where you forget to execute the function:

function example(input: any) {
    if (typeof input === 'function') {
        alert('You passed a function!');
    }
}

function someFunction() {
    return 1;
}

// Okay
example({ name: 'Zoltán' });
example(1);
example('a string');
example(someFunction());

// Not okay
example(function () {});
example(someFunction);

为什么你不能做你想做的事?

几乎可以,因为您可以使用重载来允许多种类型之一",例如:

Why can't you actually do what you want?

You almost can, because you could use an overload to allow "one of many types", for example:

class Example {
    someMethod(input: number);
    someMethod(input: string);
    someMethod(input: boolean);
    someMethod(input: any) {

    }
}

问题来了:为了允许对象类型,您必须添加 someMethod(input: Object);someMethod(input: {}); 的重载签名;.一旦你这样做,函数就会被允许,因为函数继承自对象.

Here comes the rub: in order to allow object types, you would have to add an overload signature of someMethod(input: Object); or someMethod(input: {});. As soon as you do this, functions would become allowed, because function inherits from object.

如果您可以将 object 缩小到不那么通用的范围,您可以简单地为您想要允许的所有类型添加越来越多的重载 (yikes).

If you could narrow down object to something less general, you could simply add more and more overloads (yikes) for all the types you want to allow.

这篇关于除了函数之外,TypeScript 中还有其他类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆