chore(types): improve of type assertion (#4141)

This commit is contained in:
webfansplz 2021-07-19 22:32:07 +08:00 committed by GitHub
parent 1e5e004d7c
commit df0ce21836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -692,7 +692,7 @@ export function createCallExpression<T extends CallExpression['callee']>(
loc, loc,
callee, callee,
arguments: args arguments: args
} as any } as InferCodegenNodeType<T>
} }
export function createFunctionExpression( export function createFunctionExpression(

View File

@ -17,20 +17,24 @@ export function defaultOnWarn(msg: CompilerError) {
__DEV__ && console.warn(`[Vue warn] ${msg.message}`) __DEV__ && console.warn(`[Vue warn] ${msg.message}`)
} }
type InferCompilerError<T> = T extends ErrorCodes
? CoreCompilerError
: CompilerError
export function createCompilerError<T extends number>( export function createCompilerError<T extends number>(
code: T, code: T,
loc?: SourceLocation, loc?: SourceLocation,
messages?: { [code: number]: string }, messages?: { [code: number]: string },
additionalMessage?: string additionalMessage?: string
): T extends ErrorCodes ? CoreCompilerError : CompilerError { ): InferCompilerError<T> {
const msg = const msg =
__DEV__ || !__BROWSER__ __DEV__ || !__BROWSER__
? (messages || errorMessages)[code] + (additionalMessage || ``) ? (messages || errorMessages)[code] + (additionalMessage || ``)
: code : code
const error = new SyntaxError(String(msg)) as CompilerError const error = new SyntaxError(String(msg)) as InferCompilerError<T>
error.code = code error.code = code
error.loc = loc error.loc = loc
return error as any return error
} }
export const enum ErrorCodes { export const enum ErrorCodes {