英文:
How to check that a value has specific type
问题
以下是您要翻译的部分:
I want typescript to check value to match specific type without actually invoking this
What is the most elegant way to do this rather than I have described below?
Consider the following example:
import { OdbEventProcessorFunc } from "./OdbEventProcessor";
export function tviewEventProcessor() {
// some implementation here... doesn't matter for the question
}
// The ugly way to do the job:
function unused_just_for_type_check_of_the_function() {
// The following line checks that function 'tviewEventProcessor' is actually 'OdbEventProcessorFunc' and raise TS2322 error if it's not
const unused_just_for_type_check_of_the_function2: OdbEventProcessorFunc = tviewEventProcessor;
}
The code above does what I need, and I actually use it rare cases when I need it.
But I wonder if there is a better way to do this?
Some kind of
typescript_please_check_that(tviewEventProcessor is OdbEventProcessorFunc )
What struggles me in existing approach is:
- Its ugly and long to write
- It creates some code that can be bundled. Though it should be stripped by treeshaking, but anyway
Additional Q&A:
Q: Why doing it like this and not typecheck on caller side?
A: Because when I change definition of 'OdbEventProcessorFunc' I want IDE to navigate me with TS2322 errors not to the callers of this function, but to its definiton.
英文:
I want typescript to check value to match specific type without actually invoking this
What is the most elegant way to do this rather than I have described below?
Consider the following example:
import { OdbEventProcessorFunc } from "./OdbEventProcessor";
export function tviewEventProcessor() {
// some implementation here... doesn't matter for the question
}
// The ugly way to do the job:
function unused_just_for_type_check_of_the_function() {
// The following line checks that function 'tviewEventProcessor' is actually 'OdbEventProcessorFunc' and raise TS2322 error if it's not
const unused_just_for_type_check_of_the_function2: OdbEventProcessorFunc = tviewEventProcessor;
}
The code above does what I need, and I actually use it rare cases when I need it.
But I wonder if there is a better way to do this?
Some kind of
typescript_please_check_that(tviewEventProcessor is OdbEventProcessorFunc )
What struggles me in existing approach is:
- Its ugly and long to write
- It creates some code that can be bundled. Though it should be stripped by treeshaking, but anyway
Additional Q&A:
Q: Why doing it like this and not typecheck on caller side?
A: Because when I change definition of 'OdbEventProcessorFunc' I want IDE to navigate me with TS2322 errors not to the callers of this function, but to its definiton.
答案1
得分: 2
如果您改用箭头函数(作为表达式而不是函数声明,可以轻松进行类型检查而不需要额外的复杂性),您可以同时导出并检查类型。例如:
type Fn = (arg: string) => number;
export const fn: Fn = (arg: string) => {
return 5;
};
如果您仍然使用function
,您可以使用satisfies
来进行类型检查,这不会在其他地方使用。
type Fn = (arg: string) => number;
export const fn = (arg: string) => {
return 5;
};
fn satisfies Fn;
无需将其包装在函数中,也无需在其中包含任何发出的代码。
英文:
If you use an arrow function instead (which, as an expression rather than a function declaration, can be easily type-checked without any extra baggage), you can export and check the type at the same time. For example:
type Fn = (arg: string) => number;
export const fn: Fn = (arg: string) => {
return 5;
};
If you stay with a function
, you can use a satisfies
that isn't used anywhere else to typecheck.
type Fn = (arg: string) => number;
export const fn = (arg: string) => {
return 5;
};
fn satisfies Fn;
No need to wrap it in a function, or to have any emitted code there.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论