-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed #16594: getComponent() can't support abstract class. #16917
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
import { errorID } from '../platform/debug'; | ||
import { assertIsTrue } from '../data/utils/asserts'; | ||
|
||
const hasOwnPropertyProto = Object.prototype.hasOwnProperty; | ||
|
||
export type EnumType = Record<string, string | number>; | ||
|
||
/** | ||
|
@@ -46,7 +48,7 @@ | |
* @zh 包含枚举名和值的 JavaScript literal 对象,或者是一个 TypeScript enum 类型。 | ||
* @return @en The defined enum type. @zh 定义的枚举类型。 | ||
*/ | ||
export function Enum<T> (obj: T): T { | ||
export function Enum<T extends object> (obj: T): T { | ||
if ('__enums__' in obj) { | ||
return obj; | ||
} | ||
|
@@ -61,7 +63,7 @@ | |
* 更新枚举对象的属性列表。 | ||
* @param obj @en The enum object to update. @zh 需要更新的枚举对象。 | ||
*/ | ||
Enum.update = <T> (obj: T): T => { | ||
Enum.update = <T extends object> (obj: T): T => { | ||
let lastIndex = -1; | ||
const keys: string[] = Object.keys(obj); | ||
|
||
|
@@ -93,7 +95,7 @@ | |
return obj; | ||
}; | ||
|
||
namespace Enum { | ||
export interface Enumerator<EnumT> { | ||
/** | ||
* The name of the enumerator. | ||
|
@@ -115,17 +117,17 @@ | |
* Determines if the object is an enum type. | ||
* @param enumType @en The object to judge. @zh 需要判断的对象。 | ||
*/ | ||
Enum.isEnum = <EnumT extends {}>(enumType: EnumT): boolean => enumType && enumType.hasOwnProperty('__enums__'); | ||
Enum.isEnum = <EnumT extends object>(enumType: EnumT): boolean => enumType && hasOwnPropertyProto.call(enumType, '__enums__'); | ||
|
||
function assertIsEnum <EnumT extends {}> (enumType: EnumT): asserts enumType is EnumT & EnumExtras<EnumT> { | ||
assertIsTrue(enumType.hasOwnProperty('__enums__')); | ||
function assertIsEnum <EnumT extends object> (enumType: EnumT): asserts enumType is EnumT & EnumExtras<EnumT> { | ||
assertIsTrue(hasOwnPropertyProto.call(enumType, '__enums__')); | ||
} | ||
|
||
/** | ||
* Get the enumerators from the enum type. | ||
* @param enumType @en An enum type. @zh 枚举类型。 | ||
*/ | ||
Enum.getList = <EnumT extends {}>(enumType: EnumT): readonly Enum.Enumerator<EnumT>[] => { | ||
Enum.getList = <EnumT extends object>(enumType: EnumT): readonly Enum.Enumerator<EnumT>[] => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix an eslint error:
|
||
assertIsEnum(enumType); | ||
|
||
if (enumType.__enums__) { | ||
|
@@ -140,7 +142,7 @@ | |
* @param enumType @en The enum type defined from [[Enum]] @zh 从[[Enum]]定义的枚举类型。 | ||
* @return {Object[]} | ||
*/ | ||
function updateList<EnumT extends {}> (enumType: EnumT): readonly Enum.Enumerator<EnumT>[] { | ||
function updateList<EnumT extends object> (enumType: EnumT): readonly Enum.Enumerator<EnumT>[] { | ||
assertIsEnum(enumType); | ||
const enums: any[] = enumType.__enums__ || []; | ||
enums.length = 0; | ||
|
@@ -170,7 +172,7 @@ | |
* @param enumType @en The enum type defined from [[Enum]] @zh 从[[Enum]]定义的枚举类型。 | ||
* @param compareFn @en Function used to determine the order of the elements. @zh 用于确定元素顺序的函数。 | ||
*/ | ||
Enum.sortList = <EnumT extends {}> (enumType: EnumT, compareFn: (a, b) => number): void => { | ||
Enum.sortList = <EnumT extends object> (enumType: EnumT, compareFn: (a, b) => number): void => { | ||
assertIsEnum(enumType); | ||
if (!Array.isArray(enumType.__enums__)) { | ||
return; | ||
|
@@ -200,7 +202,7 @@ | |
* @en enumType An enum type, eg, a kind of type with similar semantic defined by TypeScript. | ||
* @zh 枚举类型,例如 TypeScript 中定义的类型。 | ||
*/ | ||
export function ccenum<EnumT extends {}> (enumType: EnumT): void { | ||
export function ccenum<EnumT extends object> (enumType: EnumT): void { | ||
if (!('__enums__' in enumType)) { | ||
value(enumType, '__enums__', null, true); | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix an ESLint error:
ESLint doesn't suggest to use obj.hasOwnProperty on target object since target object may contain a same key
hasOwnProperty
to override the original behavior.